oracle-按月、日、时分组查询统计数据,无数据补零

本文介绍如何使用 Oracle SQL 对数据进行按月、日、小时的分组查询,并对空值进行补零处理。通过示例展示了如何利用 TO_CHAR 函数进行日期格式化,以及使用 NVL 函数处理可能存在的 NULL 值。

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

oracle-按月、日、时分组查询数据,为空的数据补零

------月

1
2
3
4
5
6
7
8
9
10
11
select  nvl(t1.tvalue, 0)  "data1" , t2.datevalue  "name"
   from  ( select  sum (t.TSAI03) tvalue, TO_CHAR(t.TSAI01,  'yyyy-mm' ) timevalue
           from  TSA009 t
          where  TO_CHAR(t.TSAI01,  'YYYY-MM-DD' like  '2012%'
            and  t.unit_code  like  '411500A0050000'
          group  by  TO_CHAR(t.TSAI01,  'yyyy-mm' )) t1,
        ( select  '2012-'  || lpad( level , 2, 0) datevalue
           from  dual
         connect  by  level  < 13) t2
  where  t1.timevalue(+) = t2.datevalue
  order  by  t2.datevalue

 

-----日

select  nvl(t1.tvalue, 0) "data1" , t2.datevalue "name"
   from  ( select  sum (t.TSAI03) tvalue,
                TO_CHAR(t.TSAI01, 'yyyy-mm-dd' ) timevalue
           from  TSA009 t
          where  TO_CHAR(t.TSAI01, 'YYYY-MM-DD' ) like  '2012-04%'
            and  t.unit_code like  '411500A0050000'
          group  by  TO_CHAR(t.TSAI01, 'yyyy-mm-dd' )) t1,
        ( select  '2012-04-'  || lpad( level , 2, 0) datevalue
           from  dual
         connect  by  level  < ( select  to_number(substr(last_day(to_date( '2012-04-10' ,
                                                                      'yyyy-mm-dd' )),
                                                     0,
                                                     2))
                               from  dual) + 1) t2
  where  t1.timevalue(+) = t2.datevalue
  order  by  t2.datevalue

 

----时

select  nvl(t1.tvalue, 0) "data1" , t2.datevalue "name"
   from  ( select  sum (t.TSAJ03) tvalue,
                TO_CHAR(t.TSAJ01, 'yyyy-mm-dd hh24' ) timevalue
           from  TSA010 t
          where  TO_CHAR(t.TSAJ01, 'YYYY-MM-DD' ) like  '2012-04-10%'
            and  t.unit_code like  '411500A0050000'
          group  by  TO_CHAR(t.TSAJ01, 'yyyy-mm-dd hh24' )) t1,
        ( select  '2012-04-10 '  || lpad( level , 2, 0) datevalue
           from  dual
         connect  by  level  < 25) t2
  where  t1.timevalue(+) = t2.datevalue
  order  by  t2.datevalue

为避免原文丢失,特此转载自:https://www.cnblogs.com/GenghisKhan/archive/2012/07/10/2584571.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值