sql - Calculating a running count of Weeks -
i looking calculate running count of weeks have occurred since starting point. biggest problem here calendar working on not traditional gregorian calendar.
the easiest dimension reference 'tweek' tells week of year record falls into.
example data:
create table #foobar ( datekey int ,tweek int ,cumweek int ); insert #foobar (datekey, tweek, cumweek) values(20150630, 1,1), (20150701,1,1), (20150702,1,1), (20150703,1,1), (20150704,1,1), (20150705,1,1), (20150706,1,1), (20150707,2,2), (20150708,2,2), (20150709,2,2), (20150710,2,2), (20150711,2,2), (20150712,2,2), (20150713,2,2), (20150714,1,3), (20150715,1,3), (20150716,1,3), (20150717,1,3), (20150718,1,3), (20150719,1,3), (20150720,1,3), (20150721,2,4), (20150722,2,4), (20150723,2,4), (20150724,2,4), (20150725,2,4), (20150726,2,4), (20150727,2,4)
for sake of ease, did not go way 52, point. trying recreate 'cumweek' column. have column tells me correct week of year according weird calendar convention ('tweek').
i know involve kind of over() windowing, cannot seem figure out.
the windowing function lag()
along summation of order rows between
"changes" should close enough work with. caveat order rows between
can take integer literal.
year rollover : guess create ranking level based on mod 52 start count fresh. 53 become year 2, week 1, not 53.
select * , sum(changedrow) on (order datekey rows between 99999 preceding , current row) ( select datekey, tweek, changedrow=case when lag(tweek) on (order datekey) <> tweek 1 else 0 end @foobar f2 )as detail
Comments
Post a Comment