oracle根据时间颗粒度来查询趋势图

select d.trendTime,nvl(a.trendNumber,0) trendNumber from (


hour

) a,(SELECT to_char(to_date(#{beginTime},'yyyy-mm-dd hh24:mi:ss') + (ROWNUM - 1) / 24,'yyyy-mm-dd hh24') trendTime 
	FROM dual
CONNECT BY ROWNUM <= (((to_date(#{endTime},'yyyy-mm-dd hh24:mi:ss')+0) - (to_date(#{beginTime},'yyyy-mm-dd hh24:mi:ss')+0)) * 24+1) )  d where a.trendTime(+)=d.trendTime

day

) a,(SELECT TO_CHAR(to_date(#{beginTime},'yyyy-mm-dd hh24:mi:ss')+ ROWNUM - 1, 'yyyy-MM-dd') trendTime
  FROM DUAL
CONNECT BY ROWNUM <=((to_date(#{endTime},'yyyy-mm-dd hh24:mi:ss')+0)-(to_date(#{beginTime},'yyyy-mm-dd hh24:mi:ss')+0)) + 1) d where a.trendTime(+)=d.trendTime

week

) a,(SELECT to_char(TRUNC(to_date(#{beginTime}, 'yyyy-mm-dd hh24:mi:ss') +
                     (ROWNUM - 1) * 7,
                     'D'),
               'yyyy-mm-dd') trendtime,
       0 num
  FROM DUAL
CONNECT BY ROWNUM <
= TRUNC(TRUNC((to_date(#{endTime}, 'yyyy-mm-dd hh24:mi:ss') + 0) +
              (ROWNUM - 1) * 7,
              'D') - TRUNC((to_date(#{beginTime},
                                    'yyyy-mm-dd
    hh24:mi:ss') + 0) + (ROWNUM - 1) * 7,
                           'D')) / 7 + 1) d where a.trendTime(+) = d.trendtime) order by trendTime

month

) a,(SELECT TO_CHAR(ADD_MONTHS(to_date(#{beginTime},'yyyy-mm-dd hh24:mi:ss'), ROWNUM - 1),
               'yyyy-MM') trendTime
  FROM DUAL
CONNECT BY ROWNUM <=
           ceil(months_between(to_date(#{endTime},'yyyy-mm-dd hh24:mi:ss'),to_date(#{beginTime},'yyyy-mm-dd hh24:mi:ss'))) + 1) d where a.trendTime(+)=d.trendTime
时间范围 <24小时,支持按小时统计
1天=<时间范围<7天,支持按小时、天展示
7天=<时间范围<30天,支持按天、周显示
30天=<时间范围<120天,支持按周、按月统计
120天=<时间范围,支持按月统计

 

你可能感兴趣的:(数据库)