I am trying to use the PARTITION BY OVER to 'group' rows by certain columns. I understand the use of PARTITION somewhat, however I want to 'block' the partitions by date. For example, if we have
|col1|col2 |
| A |01/JAN/2012|
| A |01/FEB/2012|
| B |01/MAR/2012|
| B |01/APR/2012|
| A |01/MAY/2012|
And I want to partition by col1 but I want the last A to be 'different' from the first two as it is separated date wise by the 'B' rows.
If I use;
SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2) AS RNUM, a.*
FROM table1 a;
It will yield;
|RNUM|col1|col2 |
| 1| A |01/JAN/2012|
| 2| A |01/FEB/2012|
| 3| A |01/MAY/2012|
| 1| B |01/MAR/2012|
| 2| B |01/APR/2012|
but what I really want is;
|RNUM|col1|col2 |
| 1| A |01/JAN/2012|
| 2| A |01/FEB/2012|
| 1| B |01/MAR/2012|
| 2| B |01/APR/2012|
| 1| A |01/MAY/2012|
Is this possible using PARTITION BY OVER? At the moment I have dropped back to using a cursor to parse over the data and assign a group id so I can separate the two sequences of 'A' but this is quite slow.
Thanks,
Mark.