I have data in this format
ColA | Date | RSN | ID | DesiredColumn |
---|---|---|---|---|
70 | 0904 | 2 | 0904-2 | 1 |
71 | 0904 | 3 | 0904-3 | 1 |
100 | 0904 | 4 | 0904-4 | 1 |
70 | 0904 | 5 | 0904-5 | 2 |
I want to add the DesiredColumn that changes its values every time it sees RTI = 070 in ColA
Can someone please help?
CodePudding user response:
You can use a cumulative sum, something like:
select t.*,
sum(case when colA = 70 then 1 else 0 end) over (partition by date order by rsn)
from t;
I am guessing that you want this per value of date and the ordering is based on rsn
. If that is not true, remove the partition by
clause.
Sql Fiddle: http://sqlfiddle.com/#!18/59e49/10