Oracle 合计统计sql语句

本文深入探讨了使用SQL进行复杂聚合查询的方法,包括如何利用GROUP BY ROLLUP进行数据汇总,以及如何结合DECODE函数实现数据的灵活展示。通过具体案例,展示了如何在不同日期范围内对特定字段进行求和,并对特定条件下的数据进行加权计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select decode(grouping(DATA_ID), 1, '合计', DATA_ID) as DATA_ID,
       sum(H1) as H1,
       sum(H2) as H2
  from (select DATA_ID, H1, H2
          from TB_REPORT_f302050700
         where DATA_ID between '20120206' and '20120213')
 group by rollup(DATA_ID);
 


 
 select decode(grouping(outid),1,'合計',outid) outid,name,sum(sumopfare),
sum(case when sumopfare>=50 then 100 else sumopfare*2 end)
from (select bc.outid,bc.name,
      sum(t.opfare) sumopfare,
      from rec_cust_acc t,base_customers bc 
      where t.acccode = 101 and t.customerid = bc.customerid
      and t.opdt between 
                to_date(concat('2010-08-01',' 00:00:00'),'yyyy-mm-dd hh24:mi:ss') 
            and to_date(concat('2010-12-06',' 23:59:59'),'yyyy-mm-dd hh24:mi:ss') 
      group by bc.outid,bc.name,t.opfare)
group by rollup((outid,name))
order by outid,name;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值