See my approach below, it's simmilar to Dazzal's answer, a little different logic:
SQL FIDDLE
Step1:
--find the swhitches to new groups
select col1, col2,
case when nvl(lag(col1) over (order by col2),sysdate) <> col1 then 1 end as new_grp
from data;
COL1 COL2 NEW_GRP
A January, 01 2012 1
A February, 01 2012 (null)
B March, 01 2012 1
B April, 01 2012 (null)
A May, 01 2012 1
Step2:
--identify/mark the groups
select col1, col2, sum(new_grp) over (order by col2) as grp
from(
select col1, col2,
case when nvl(lag(col1) over (order by col2),sysdate) <> col1 then 1 end as new_grp
from data)
;
COL1 COL2 NEW_GRP
A January, 01 2012 1
A February, 01 2012 1
B March, 01 2012 2
B April, 01 2012 2
A May, 01 2012 3
Step3:
--find the row_number within group
select col1, col2, row_number() over(partition by grp order by col2) rn
from(
select col1, col2, sum(new_grp) over (order by col2) as grp
from(
select col1, col2,
case when nvl(lag(col1) over (order by col2),sysdate) <> col1 then 1 end as new_grp
from data
)
);
COL1 COL2 NEW_GRP
A January, 01 2012 1
A February, 01 2012 2
B March, 01 2012 1
B April, 01 2012 2
A May, 01 2012 1