I am working on one code where I need to add sequential number as per the grouping on the basis of column A & column B. Below is the table/dataframe I have. The data is sorted by colA & Date.
colA | colB | Date |
---|---|---|
A | 1 | 01-01-2014 |
A | 1 | 01-02-2014 |
A | 3 | 30-04-2014 |
A | 3 | 05-05-2014 |
A | 2 | 25-05-2014 |
A | 1 | 06-06-2014 |
A | 1 | 21-07-2014 |
B | 1 | 04-09-2014 |
B | 1 | 19-10-2014 |
B | 1 | 03-12-2014 |
C | 3 | 17-01-2015 |
C | 2 | 03-03-2015 |
C | 2 | 17-04-2015 |
Following is the expected result:
colA | colB | Date | ROWNUM |
---|---|---|---|
A | 1 | 01-01-2014 | 1 |
A | 1 | 01-02-2014 | 2 |
A | 3 | 30-04-2014 | 1 |
A | 3 | 05-05-2014 | 2 |
A | 2 | 25-05-2014 | 1 |
A | 1 | 06-06-2014 | 1 |
A | 1 | 21-07-2014 | 2 |
B | 1 | 04-09-2014 | 1 |
B | 1 | 19-10-2014 | 2 |
B | 1 | 03-12-2014 | 3 |
C | 3 | 17-01-2015 | 1 |
C | 2 | 03-03-2015 | 1 |
C | 2 | 17-04-2015 | 2 |
I am trying to use row_number here and getting the below result where for A(colA) the next occurrence of 1(colB) has rownumber added as 3 instead of 1:
colA | colB | Date | ROWNUM |
---|---|---|---|
A | 1 | 01-01-2014 | 1 |
A | 1 | 01-02-2014 | 2 |
A | 3 | 30-04-2014 | 1 |
A | 3 | 05-05-2014 | 2 |
A | 2 | 25-05-2014 | 1 |
A | 1 | 06-06-2014 | 3 |
A | 1 | 21-07-2014 | 4 |
B | 1 | 04-09-2014 | 1 |
B | 1 | 19-10-2014 | 2 |
B | 1 | 03-12-2014 | 3 |
C | 3 | 17-01-2015 | 1 |
C | 2 | 03-03-2015 | 1 |
C | 2 | 17-04-2015 | 2 |