WITH t AS ( SELECT 1 seq, 'A' name, 10 qty FROM dual UNION ALL SELECT 2, 'A', 10 FROM dual UNION ALL SELECT 3, 'A', 10 FROM dual UNION ALL SELECT 4, 'B', 10 FROM dual UNION ALL SELECT 5, 'B', 10 FROM dual UNION ALL SELECT 6, 'A', 10 FROM dual UNION ALL SELECT 7, 'A', 10 FROM dual UNION ALL SELECT 8, 'A', 10 FROM dual ) SELECT MIN(seq) min_seq , name , SUM(qty) sum_qty FROM (SELECT seq, name, qty , ROW_NUMBER() OVER(ORDER BY seq) - ROW_NUMBER() OVER(PARTITION BY name ORDER BY seq) gb FROM t ) GROUP BY gb, name ORDER BY min_seq ;