sql 请教,求N行的合计,不要小计
如数据为:
a b c
a1 张三 100
b1 李四 200
c1 王五 300
要求结果为:
a b c
a1 张三 100
b1 李四 200
c1 王五 300
合计 600
------解决方案--------------------
with t1 as
(
select 'a1' a,'张三' b,100 c from dual union all
select 'b1' a,'李四' b,200 c from dual union all
select 'c1' a,'王五' b,300 c from dual
)
select nvl(a,'合计') a,b,c
from
(
select a,b,sum(c) c
from t1
group by rollup(a,b)
)
where not (a is not null and b is null)
a b c
--------------------------------
1 a1 张三 100
2 b1 李四 200
3 c1 王五 300
4 合计 600
------解决方案--------------------
select nvl(a, '合计'), b, sum(c) c from t1 group by rollup((a, b, c))