惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 【当耐特】
A
About on SuperTechFans
Last Week in AI
Last Week in AI
雷峰网
雷峰网
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements

博客园 - FilyCks

SQL动态条件查询例子 日期格式化函数 解决日志文件满造成的无法写入问题 数据库的日志文件 Delphi制作DLL SQLSERVER 日期函数及取当天数据 SQL语句导入导出大全 日期数据处理 FastReport(5) FastReport(4) FastReport(3) FastReport(2) FastReport(1) 游标嵌套(个人笔记) FastReport 的初学感悟 查询表(个人笔记) 图片转换和读取(个人笔记) 游标的一些例子 自己研究出来的第一个游标
按时间段统计数据(1)
FilyCks · 2008-03-17 · via 博客园 - FilyCks

有这样一个表table1,两个字段,createtime和value,如下

2007-07-09   07:00:01           90
2007-07-09   08:05:31           78
2007-07-09   14:00:00           95
2007-07-09   21:15:01           20
2007-07-10   22:23:08           10

这个表每天,每分钟都会有数据。统计每天0点到8点,21点到23,value的总和,其它时间段不要。

--测试数据
create   table   t4(cdate   date,cnum   int)
insert   into   t4
select   to_date( '2007-07-09   07:00:01 ', 'yyyy-mm-dd   hh24:MI:SS) '),90   from   dual   union   all  
select   to_date( '2007-07-09   08:05:31 ', 'yyyy-mm-dd   hh24:MI:SS) '),78   from   dual   union   all
select   to_date( '2007-07-09   14:00:00 ', 'yyyy-mm-dd   hh24:MI:SS) '),95   from   dual   union   all
select   to_date( '2007-07-09   21:15:01 ', 'yyyy-mm-dd   hh24:MI:SS) '),20   from   dual   union   all
select   to_date( '2007-07-10   22:23:08 ', 'yyyy-mm-dd   hh24:MI:SS) '),10   from   dual;
--执行查询
select   to_char(cdate, 'yyyy-mm-dd '),
sum(case   when   to_number(to_char(   cdate, 'hh24 '   ))> 0   and   to_number(to_char(   cdate, 'hh24 '   )) <=8   then   cnum   else   0   end)   "0~8 ",
sum(case   when   to_number(to_char(   cdate, 'hh24 '   ))> 21   and   to_number(to_char(   cdate, 'hh24 '   )) <=23   then   cnum   else   0   end)   "21~23 "
from   t4
group   by   to_char(cdate, 'yyyy-mm-dd ')
--查询结果
2007-07-09 168 0
2007-07-10 0 10