[Solved] Need Row Count to Restart at the beginning of each member in a BY
Hey All,
I am trying to calculate the number of rows based on a DB_EXPR command and it's working great. However, I would like it to restart the logic from the beginning of each Calendar Year.
For example right now my expression generates results like:
1. 2016: 1,2,3,4,5 2. 2017: 6,7,8,9,10
I would like 2017 to restart at 1. Is there any way to do this? Here is the code that I am working with:
TABLE FILE _ADM_STAR
SUM
SALES
COMPUTE ROW_CNT/I4=DB_EXPR(ROW_NUMBER() OVER (PARTITION BY "CALENDARYEAR" ORDER BY "CALENDARYEAR","SALES" desc));
BY CALENDARYEAR
BY PROVIDERNAME
BY PROVIDERTAXID
WHERE CALENDARYEAR EQ '2015' OR '2016' OR '2017'
END
Thanks in advance!!This message has been edited. Last edited by: Brandon Andrathy,
WebFOCUS 8204
October 25, 2019, 07:28 PM
Hallway
I'm not 100% sure of what you're looking for. But, maybe look at the PARTITION_AGGR function.
Example below:
DEFINE FILE ggsales
CALENDARYEAR/YY=DATE ;
END
TABLE FILE ggsales
SUM DOLLARS
COMPUTE ROW_CNT/I11=PARTITION_AGGR(CNT.PCD, CALENDARYEAR, B, C, CNT);
BY CALENDARYEAR
BY PRODUCT
BY PCD
ON TABLE PCHOLD FORMAT HTML
ON TABLE SET PAGE-NUM OFF
ON TABLE SET STYLE *
INCLUDE=IBFS:/FILE/IBI_HTML_DIR/ibi_themes/flat.sty,$
TYPE=REPORT, LINES-PER-PAGE=UNLIMITED, $
ENDSTYLE
END
-RUN
Hallway
Prod: 8202M1
Test: 8202M4
Repository:
OS:
Outputs:
October 26, 2019, 04:42 PM
csswalt
Your SQL Server ROW_NUMBER function looks correct. The PARTITION BY part of it is the column that you want to reset back to 1 when the value changes.
Look at this code: SQL SQLMSS SELECT YEAR(CREATE_DATE) AS CREATED_YEAR, ROW_NUMBER() OVER(PARTITION BY YEAR(CREATE_DATE) order by year(create_date) ) AS Row#, name, recovery_model_desc FROM sys.databases t1 WHERE database_id < 10 ORDER BY CREATED_YEAR; END
Notice that for the change of each year the Row# goes back to 1.
Look at your translated SQL and make sure the translation of the ROW_NUMBER function looks correct. If it does and you still are not getting what you think is correct, please open a case.
Walter Brengel Information Builders Technical Director of FOCUS Support Services
FOCUS Virtual User Group Please join us (Walter Blood and me) and other FOCUS/WebFOCUS coders for the next FOCUS Virtual User Group meeting.
Apologies if I am oversimplifying this, but if I were to use only WebFOCUS code, then I would simply do this:
COMPUTE ROW_CNT/I4=IF CALENDARYEAR NE LAST CALENDARYEAR THEN 1 ELSE ROW_CNT + 1 ;
WebFOCUS 8.2.06 mostly Windows Server
October 28, 2019, 09:56 AM
csswalt
Hi Twanette,
Your suggestion and Hallway's are absolutely correct.
In my original involvement with this, the customer was trying to create WebFOCUS code based on complex select statement that contained subqueries. The subquery was using the ROW_NUMBER function which they wanted to continue to use.
The only difference between what you (and Hallway) posted and using DB_EXPR to use the ROW_NUMBER fucntion is where the work will be done (WebFOCUS side vs RDMBS).
Walter Brengel Information Builders Technical Director of FOCUS Support Services
FOCUS Virtual User Group Please join us (Walter Blood and me) and other FOCUS/WebFOCUS coders for the next FOCUS Virtual User Group meeting.
This has been solved. Turns out the reason it wasn't working was because Calendar Year was an integer and was being summed so the logic in the partition was never being met. Once we took the max year in a compute and then referenced that in the partition it worked beautifully. Thank you Walt and everyone else for your time with this!!