I have two columns ID and FLAG as :
ID FLAG
1 Y
1 N
1 Y
1 N
1 N
1 N
1 N
1 Y
1 N
2 N
2 Y
2 N
2 N
2 Y
2 Y
2 N
Required Output:
ID FLAG REQ_COL
1 Y null
1 N 1
1 Y null
1 N 1
1 N 2
1 N 3
1 N 4
1 Y null
1 N 1
2 N null
2 Y null
2 N 1
2 N 2
2 Y null
2 Y null
2 N 1
Logic for required column :
- For first occurence of ID , REQ_COL is null
- If FLAG=Y then REQ_COL is null
- IF previous value of FLAG is Y and there are consecutive N in FLAG , then the REQ_COL values are 1,2,3,4, 5.....
- Else Null
I am trying to apply conditional row_number but not getting idea how to proceed. Please help.