SQL> select * from t1_ order by a,b; A B - - a w a w a x a y b x b z b z b z c x c x c y A B - - d y d z 13 rows selected. SQL> SQL> -- Get the count group by a,b first SQL> -- and then sum them up, order them and pick the first row in each partition to return. SQL> with v1 as (select a, b, count(1) cnt from t1_ group by a,b order by 1,3 desc) 2 select a, b, sum(cnt) over (partition by a) total, cnt, 3 row_number() over (partition by a order by cnt desc) rnk from v1; A B TOTAL CNT RNK - - ---------- ---------- ---------- a w 4 2 1 a x 4 1 2 a y 4 1 3 b z 4 3 1 b x 4 1 2 c x 3 2 1 c y 3 1 2 d y 2 1 1 d z 2 1 2 9 rows selected. SQL> SQL> with v1 as (select a, b, count(1) cnt from t1_ group by a,b order by 1,3 desc) 2 select a, total, b, cnt from ( 3 select a, b, sum(cnt) over (partition by a) total, cnt, 4 row_number() over (partition by a order by cnt desc) rnk from v1) 5 where rnk=1; A TOTAL B CNT - ---------- - ---------- a 4 w 2 b 4 z 3 c 3 x 2 d 2 y 1 SQL> spool off