原始数据: A B C a1 b1 1
a1 b1 1
a1 b2 1
a1 b2 1
a1 b2 1
a2 b3 1
a2 b4 1
a2 b5 1
a2 b5 1
...... 汇总结果: --------------------------
a1 5
b1 2
b2 3
--------------------------
a2 4
b3 1
b4 1
b5 2
--------------------------
......
a1 b1 1
a1 b2 1
a1 b2 1
a1 b2 1
a2 b3 1
a2 b4 1
a2 b5 1
a2 b5 1
...... 汇总结果: --------------------------
a1 5
b1 2
b2 3
--------------------------
a2 4
b3 1
b4 1
b5 2
--------------------------
......
select case when type=1 then a else null end as a,
case when type=2 then b else null end as b,
c
from (
select a,min(b) as b,sum(c) as c ,1 as type
from tablename group by a
union all
select a,b,sum(c) as c ,2 as type
from tablename group by a,b
) as x
order by a,type,b