This can be done using the Tabibitosan method. Run this to figure it out:
with a as( select 1 slno, 'A' pg from dual union all select 2 slno, 'A' pg from dual union all select 3 slno, 'B' pg from dual union all select 4 slno, 'A' pg from dual union all select 5 slno, 'A' pg from dual union all select 6 slno, 'A' pg from dual ) select slno, pg, newgrp, sum(newgrp) over (order by slno) grp from( select slno, pg, case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp from a );
Newgrp means a new group has been found.
Result:
SLNO PG NEWGRP GRP 1 A 1 1 2 A 0 1 3 B 1 2 4 A 1 3 5 A 0 3 6 A 0 3
Now just use the by with count group to find the group with the maximum number of occurrences:
with a as( select 1 slno, 'A' pg from dual union all select 2 slno, 'A' pg from dual union all select 3 slno, 'B' pg from dual union all select 4 slno, 'A' pg from dual union all select 5 slno, 'A' pg from dual union all select 6 slno, 'A' pg from dual ), b as( select slno, pg, newgrp, sum(newgrp) over (order by slno) grp from( select slno, pg, case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp from a ) ) select max(cnt) from ( select grp, count(*) cnt from b group by grp );
source share