oracle统计一段时间内每天的数据量,无数据是自动补零!

 
select a.time,nvl(b.num,0) as num from (
select to_char(to_date('2018/07/20', 'yyyy-mm-dd') + rownum - 1,
               'yyyy-mm-dd') as time
from dual
connect by rownum <=
           trunc(to_date('2018/08/20', 'yyyy/mm/dd') - to_date('2018/07/20', 'yyyy/mm/dd')) + 1)a
left join
                (select time,count(time) as num from (SELECT to_char(INPUT_DATE,'yyyy-mm-dd') as time FROM ORDER_REGISTRATION_  where INPUT_DATE between to_date('2018/07/20', 'yyyy/mm/dd')
  and to_date('2018/08/20', 'yyyy/mm/dd') + 1) group by time order by time)b on a.time = b.time order by a.time;

其中有三个select,,第二个select是将时间段生成日期,第二个select是将表中的不为0的数据量统计出来,然后用left join连接起来,在用第一个select将符合条件的查出来,无数据的自动补零

有需要添加限制条件的在第三个select中添加即可

折腾死我了!

 

具体例子:

查询一段时间内的锁号量和挂号量

锁号量为registeration表中每天的数据条数;

挂号量为order_表中每天status=3的数据条数;

代码实现:

select a.time,nvl(b.sum,0) as bnum,nvl(c.sum,0) as cnum from
      (select to_char(to_date('2018/10/20', 'yyyy-mm-dd') + rownum - 1,'yyyy-mm-dd') as time from dual
       connect by rownum <= trunc(to_date('2018/11/22', 'yyyy/mm/dd') - to_date('2018/10/20', 'yyyy/mm/dd')) + 1)a,
      (select time,count(time) as sum from
        (SELECT to_char(INPUT_DATE,'yyyy-mm-dd') as time FROM ORDER_REGISTRATION_
         where INPUT_DATE between to_date('2018/10/20', 'yyyy/mm/dd') and to_date('2018/11/22', 'yyyy/mm/dd') + 1) group by time)b,
      (select time,count(time) as sum from
        (SELECT to_char(INPUT_DATE,'yyyy-mm-dd') as time FROM ORDERS_  where INPUT_DATE between
          to_date('2018/10/20', 'yyyy/mm/dd') and to_date('2018/11/22', 'yyyy/mm/dd') + 1 and STATUS='3') group by time)c
       where a.time=b.time(+) and a.time=c.time(+) order by a.time;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值