oracle按年月日统计,无数据补0

select b.DAY_TIME xaxis, nvl(a.yaxis,0) yaxis from ( select substr(o.CREATEDATE, 1,4) xaxis, count(*) yaxis from T_YY_ORDERDETAIL od inner join T_YY_ORDER o on od.ORDERID=o.ORDERID inner join T_YY_SERVICEINTERFACE sif on sif.ID=od.SERVICEID WHERE o.ORDERSERVICETYPE=7 group by substr(o.CREATEDATE, 1,4)) a

//上面是正常的统计,下面是处理日期,日期处理完成后用right join连接,然后用nvl将无数据的设置为0

right join (SELECT TO_CHAR(add_months(TO_DATE((select min(substr(o.CREATEDATE, 1, 4)) xaxis from T_YY_ORDER o), 'YYYY'),(ROWNUM - 1)*12),'YYYY') DAY_TIME FROM DUAL CONNECT BY (months_between(TO_DATE(?, 'YYYY'),TO_DATE((select min(substr(o.CREATEDATE, 1, 4)) xaxis from T_YY_ORDER o), 'YYYY')))/12 >= ROWNUM-1 ) b on a.xaxis= b.DAY_TIME order by b.DAY_TIME asc 

月:

select b.DAY_TIME xaxis, nvl(a.yaxis,0) yaxis from ( select substr(o.CREATEDATE, 1,7) xaxis, count(*) yaxis from T_YY_ORDERDETAIL od inner join T_YY_ORDER o on od.ORDERID=o.ORDERID inner join T_YY_SERVICEINTERFACE sif on sif.ID=od.SERVICEID WHERE o.ORDERSERVICETYPE=7 group by substr(o.CREATEDATE, 1,7)) a

//上面是正常的统计,下面是处理日期,日期处理完成后用right join连接,然后用nvl将无数据的设置为0

right join (SELECT TO_CHAR(add_months(TO_DATE((select min(substr(o.CREATEDATE, 1, 7)) xaxis from T_YY_ORDER o), 'YYYY-MM'), (ROWNUM - 1)), 'YYYY-MM') DAY_TIME FROM DUAL CONNECT BY (months_between(TO_DATE(?, 'YYYY-MM'),TO_DATE((select min(substr(o.CREATEDATE, 1, 7)) xaxis from T_YY_ORDER o), 'YYYY-MM'))) >= ROWNUM-1 ) b on a.xaxis= b.DAY_TIME order by b.DAY_TIME asc 
 

日:

select b.DAY_TIME xaxis, nvl(a.yaxis,0) yaxis from ( select substr(o.CREATEDATE, 1,10) xaxis, count(*) yaxis from T_YY_ORDERDETAIL od inner join T_YY_ORDER o on od.ORDERID=o.ORDERID inner join T_YY_SERVICEINTERFACE sif on sif.ID=od.SERVICEID WHERE o.ORDERSERVICETYPE=7 group by substr(o.CREATEDATE, 1,10)) a

//上面是正常的统计,下面是处理日期,日期处理完成后用right join连接,然后用nvl将无数据的设置为0

right join (SELECT TO_CHAR(TO_DATE((select min(substr(o.CREATEDATE, 1, 10)) xaxis from T_YY_ORDER o), 'YYYY-MM-DD') + ROWNUM - 1, 'YYYY-MM-DD') DAY_TIME FROM DUAL CONNECT BY (TO_DATE(?, 'YYYY-MM-DD') - TO_DATE((select min(substr(o.CREATEDATE, 1, 10)) xaxis from T_YY_ORDER o), 'YYYY-MM-DD')) >= ROWNUM ) b on a.xaxis= b.DAY_TIME order by b.DAY_TIME asc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值