专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Oracle管理

oracle 怎样实现每2小时分组对数据统计,该如何处理

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
oracle 怎样实现每2小时分组对数据统计
现有视图如下:
cwar        trdt                                       num
A              2015-03-01  08:05:27     3
A              2015-03-01  08:25:37     2
A              2015-03-01  08:33:15     3
A              2015-03-01  09:04:09     5
A              2015-03-01  10:17:06     4
A              2015-03-01  10:22:09     1
A              2015-03-01  11:39:00     3
请问大侠,同一日期下每两小时对num进行分组统计要如何写,先行谢过!
------解决思路----------------------
with t as
     ( select 'A' as cwar, to_date('2015-03-01 08:05:27','yyyy-mm-dd hh24:mi:ss') as trdt, 3 as num from dual
       union all select 'A', to_date('2015-03-01 08:25:37','yyyy-mm-dd hh24:mi:ss'), 2 from dual
       union all select 'A', to_date('2015-03-01 08:33:15','yyyy-mm-dd hh24:mi:ss'), 3 from dual
       union all select 'A', to_date('2015-03-01 09:04:09','yyyy-mm-dd hh24:mi:ss'), 5 from dual
       union all select 'A', to_date('2015-03-01 10:17:06','yyyy-mm-dd hh24:mi:ss'), 4 from dual
       union all select 'A', to_date('2015-03-01 10:22:09','yyyy-mm-dd hh24:mi:ss'), 1 from dual
       union all select 'A', to_date('2015-03-01 11:39:00','yyyy-mm-dd hh24:mi:ss'), 3 from dual )
select t.cwar, b.trdt, sum(t.num) as num
  from t,
       ( select level as rn, to_date('2015-03-01','yyyy-mm-dd') + (level - 1)/ 12 as trdt from dual connect by level <= 12 ) a, 
       ( select level as rn, to_date('2015-03-01','yyyy-mm-dd') + (level)/ 12 as trdt from dual connect by level <= 12 ) b
 where b.rn = a.rn
   and t.trdt >= a.trdt
   and t.trdt < b.trdt       
 group by t.cwar, b.trdt    
 order by 1,2 
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: