SQL Server Row_Count with Reset -
i have 6 columns:
date, account, side, symbol, currency, reset flag (0 yes, 1 no) i want row count on partition reset row count whenever 0 appears in reset flag column. first 5 columns not unique combination forms unique set of columns.
please me !
every other solution i've researched doesn't work reason :/
this gaps , islands style problem. without sample data or desired results...
using 2 row_number() identify groups reset flag, , in outer query number rows resetflag , grp created in inner query.
change order of date, account, side, symbol, currency whichever order of columns want number rows by; keep them in same order each of 3 row_number()s.
/* ----- */ select date , account , side , symbol , currency , resetflag , rn = case when resetflag = 0 0 else row_number() on ( partition resetflag, grp order date, account, side, symbol, currency) end ( select * , grp = row_number() on (order date, account, side, symbol, currency) - row_number() on ( partition resetflag order date, account, side, symbol, currency) t ) s order date, account, side, symbol, currency rextester demo: http://rextester.com/vlco32635
returns:
+------------+---------+------+--------+----------+-----------+----+ | date | account | side | symbol | currency | resetflag | rn | +------------+---------+------+--------+----------+-----------+----+ | 2017-01-01 | 7 | 2 | 3 | 7,0000 | 1 | 1 | | 2017-01-02 | 8 | 9 | 9 | 6,0000 | 1 | 2 | | 2017-01-03 | 4 | 1 | 5 | 6,0000 | 1 | 3 | | 2017-01-04 | 5 | 4 | 8 | 5,0000 | 0 | 0 | | 2017-01-05 | 2 | 1 | 3 | 1,0000 | 1 | 1 | | 2017-01-06 | 8 | 0 | 2 | 0,0000 | 0 | 0 | | 2017-01-07 | 0 | 3 | 8 | 9,0000 | 1 | 1 | | 2017-01-08 | 0 | 3 | 1 | 3,0000 | 1 | 2 | +------------+---------+------+--------+----------+-----------+----+
Comments
Post a Comment