oracle 怎样实现每2小时分组对数据统计

nathanjiang 2015-03-09 01:54:24
现有视图如下:
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进行分组统计要如何写,先行谢过!
...全文
806 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nathanjiang 2015-03-09
  • 打赏
  • 举报
回复
引用 1 楼 wenyzh 的回复:
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
回复很给力,谢谢!
  • 打赏
  • 举报
回复 2
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
目录 全面认识Oracle数据库 1 Oracle数据倒入EXCEL中的方法 3 Oracle 9i如何设置时间间隔型数据 3 配置Oracle监听器 5 Oracle数据库基本操作 6 影响Oracle系统性能初始化参数 9 高性能动态SQL 11 保持Oracle数据优良性能 12 提高Oracle数据库查询统计速度 14 Oracle中巧取指定记录 14 Oracle数据安全 15 Oracle字段上建立并使用索引 29 用Windows脚本宿主自动化Oracle工具 31 进程结构和内存结构 32 Oracle监控数据库性能的SQL汇总 36 Oracle如何精确计算row的大小 38 PL/SQL编程 39 数据库的分组问题 41 oracle知识 42 数据库的导入导出 42 Oracle查询中rownum与Order by查询 45 oracle9i小结 46 Oracle 数据库的聚簇技术 61 数据库、服务名、实例 63 Oracle内存结构 64 sys用户和system用户 67 Oracle SQL语句 67 GROUPING SETS分组 74 Oracle外部程序触发 75 Oracle数据库的备份与恢复 77 Oracle分区功能提高应用程序性能 79 Oracle数据库处理时间基本准则 81 入侵Oracle数据库常用操作命令 82 Oracle数据库优化及其应用程序研究 83 Instance实例和数据库 85 Oracle数据缓冲区内部机制 85 Oracle 9i数据库密码重用规则分析 87 Oracle数据库空间管理方法 91 用SQL*Loader将Excel数据导出到Oracle 94 Oracle Spatial数据加密问题的研究 95 提高Oracle数据库系统Import的性能 96 Oracle数据库的四种启动方式 97 oracle数据库优化基础 97

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧