Hi @Ásgeir Gunnarsson ,
Thank you so much for posting here.
Please refer below simple way:
create table mytable ( [date] date, CustId int, Amount int,[trigger] bit)
insert mytable values
( '01-11-2020', 200, 3535, 1 ),
( '02-11-2020', 200, 0, 0 ),
( '03-11-2020', 200, 0,0 ),
( '04-11-2020', 200, 0,0 ),
( '05-11-2020', 200, 4162, 1 ),
( '06-11-2020', 200, 4162,1 ),
( '07-11-2020', 200, 4162,1 ),
( '08-11-2020', 200, 4162,1 ),
( '01-11-2020', 300, 0, 0 ),
( '02-11-2020', 300, 800, 1 ),
( '03-11-2020', 300, 0,0 ),
( '04-11-2020', 300, 0,0 ),
( '05-11-2020', 300, 1600,1 ),
( '06-11-2020', 300, 1600,1 ),
( '07-11-2020', 300, 1600,1 ),
( '08-11-2020', 300, 0,0 )
select *,
case when [trigger]=1 then row_number() over (partition by CustId,Amount,[trigger] order by [Date]) else 0 end [COLUMN I WANT]
from mytable
order by CustId,date
Output:
date CustId Amount trigger COLUMN I WANT
2020-11-01 200 3535 1 1
2020-11-02 200 0 0 0
2020-11-03 200 0 0 0
2020-11-04 200 0 0 0
2020-11-05 200 4162 1 1
2020-11-06 200 4162 1 2
2020-11-07 200 4162 1 3
2020-11-08 200 4162 1 4
2020-11-01 300 0 0 0
2020-11-02 300 800 1 1
2020-11-03 300 0 0 0
2020-11-04 300 0 0 0
2020-11-05 300 1600 1 1
2020-11-06 300 1600 1 2
2020-11-07 300 1600 1 3
2020-11-08 300 0 0 0
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hot issues November--What can I do if my transaction log is full?
Hot issues November--How to convert Profiler trace into a SQL Server table