oracle 小计合计查询问题? 10
在一张表中有IDCODENUMBER三个字段我想输出小计合计,但是又不想sum()....groupby请问怎么写语句?...
在一张表中 有ID CODE NUMBER三个字段
我想输出小计合计,但是又不想sum().... group by
请问怎么写语句? 展开
我想输出小计合计,但是又不想sum().... group by
请问怎么写语句? 展开
2个回答
展开全部
不知道你所谓小计是什么意思,你可以看看下面的效果
select case when ID is null then '合计'
when CODE is null then '小计' else to_char(ID) end,
CODE,total
from (
select ID,CODE,sum(NUMBER) total from tttt
group by rollup(ID,CODE)
)
如果不是上面的意思 那么用开窗函数看看
SELECT ID,CODE,NUMBER
sum(NUMBER) OVER(partition by ID,CODE ORDER BY NUMBER ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
FROM tttt;
select case when ID is null then '合计'
when CODE is null then '小计' else to_char(ID) end,
CODE,total
from (
select ID,CODE,sum(NUMBER) total from tttt
group by rollup(ID,CODE)
)
如果不是上面的意思 那么用开窗函数看看
SELECT ID,CODE,NUMBER
sum(NUMBER) OVER(partition by ID,CODE ORDER BY NUMBER ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
FROM tttt;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询