I want to increment a column and then stop and start again based on a value in another column.
For e.g:
I have a table:
CustomerID  Reportdate  True_False
9001        2013-01-01  0
9001        2013-02-01  0
9001        2013-03-01  0
9001        2013-04-01  1
9001        2013-05-01  0
9001        2013-06-01  1
9001        2013-07-01  1
9001        2013-08-01  0
9001        2013-09-01  1
9001        2013-10-01  0
9001        2013-11-01  0
9001        2013-12-01  0
9001        2014-01-01  1
9001        2014-02-01  1
9001        2014-03-01  0
9001        2014-04-01  0
9001        2014-05-01  0
9001        2014-06-01  0
9001        2014-07-01  0
9001        2014-08-01  0
9001        2014-09-01  1
9001        2014-10-01  1
9001        2014-11-01  1
9001        2014-12-01  1
9002        2013-01-01  0
9002        2013-02-01  0
9002        2013-03-01  0
9002        2013-04-01  1
9002        2013-05-01  1
9002        2013-06-01  1
9002        2013-07-01  0
9002        2013-08-01  1
9002        2013-09-01  0
9002        2013-10-01  1
9002        2013-11-01  1
9002        2013-12-01  1
9002        2014-01-01  1
9002        2014-02-01  0
9002        2014-03-01  0
9002        2014-04-01  0
9002        2014-05-01  0
9002        2014-06-01  0
9002        2014-07-01  1
9002        2014-08-01  1
9002        2014-09-01  1
9002        2014-10-01  0
9002        2014-11-01  1
9002        2014-12-01  0
The desired output:
CustomerID  Reportdate  True_False  Sequence
9001        2013-01-01  0           1 
9001        2013-02-01  0           2
9001        2013-03-01  0           3
9001        2013-04-01  1           0
9001        2013-05-01  0           1
9001        2013-06-01  1           0
9001        2013-07-01  1           0
9001        2013-08-01  0           1
9001        2013-09-01  1           0
9001        2013-10-01  0           1
9001        2013-11-01  0           2
9001        2013-12-01  0           3 
9001        2014-01-01  1           0
9001        2014-02-01  1           0
9001        2014-03-01  0           1
9001        2014-04-01  0           2
9001        2014-05-01  0           3 
9001        2014-06-01  0           4
9001        2014-07-01  0           5 
9001        2014-08-01  0           6
9001        2014-09-01  1           0
9001        2014-10-01  1           0
9001        2014-11-01  1           0
9001        2014-12-01  1           0
9002        2013-01-01  0           1
9002        2013-02-01  0           2
9002        2013-03-01  0           3
9002        2013-04-01  1           0
9002        2013-05-01  1           0
9002        2013-06-01  1           0
9002        2013-07-01  0           1
9002        2013-08-01  1           0
9002        2013-09-01  0           1
9002        2013-10-01  1           0
9002        2013-11-01  1           0
9002        2013-12-01  1           0
9002        2014-01-01  1           0
9002        2014-02-01  0           1
9002        2014-03-01  0           2
9002        2014-04-01  0           3
9002        2014-05-01  0           4  
9002        2014-06-01  0           5
9002        2014-07-01  1           0
9002        2014-08-01  1           0
9002        2014-09-01  1           0
9002        2014-10-01  0           1
9002        2014-11-01  1           0
9002        2014-12-01  0           1
So the sequence-field increment its operand by 1 where "True_False" is 0 then stops at where "True_False" is 1 and subsequently repeats the incrementation to create the sequence shown in the desired output.
All help is welcome,
CREATE TABLE test ( CustomerID bigint, DateKey date, True_false bit, ); insert into test values (9001,'2013-01-01','1'), (9001,'2013-02-01','0'), (9001,'2013-03-01','0'), (9001,'2013-04-01','0'), (9001,'2013-05-01','1'), (9001,'2013-06-01','1'), (9001,'2013-07-01','0'), (9001,'2013-08-01','0'), (9001,'2013-09-01','0'), (9001,'2013-10-01','0'), (9001,'2013-11-01','0'), (9001,'2013-12-01','1'), (9001,'2014-01-01','1'), (9001,'2014-02-01','0'), (9001,'2014-03-01','1'), (9001,'2014-04-01','0'), (9001,'2014-05-01','0'), (9001,'2014-06-01','1'), (9001,'2014-07-01','1'), (9001,'2014-08-01','0'), (9001,'2014-09-01','1'), (9001,'2014-10-01','0'), (9002,'2014-11-01','0'), (9002,'2014-12-01','0'), (9002,'2013-01-01','0'), (9002,'2013-02-01','0'), (9002,'2013-03-01','0'), (9002,'2013-04-01','1'), (9002,'2013-05-01','1'), (9002,'2013-06-01','0'), (9002,'2013-07-01','1'), (9002,'2013-08-01','1'), (9002,'2013-09-01','1'), (9002,'2013-10-01','1'), (9002,'2013-11-01','0'), (9002,'2013-12-01','1'), (9002,'2014-01-01','1'), (9002,'2014-02-01','0'), (9002,'2014-03-01','1'), (9002,'2014-04-01','1'), (9002,'2014-05-01','1'), (9002,'2014-06-01','0'), (9002,'2014-07-01','1'), (9002,'2014-08-01','1'), (9002,'2014-09-01','0'), (9002,'2014-10-01','0'), (9002,'2014-11-01','0'), (9002,'2014-12-01','0')