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

推荐订阅源

MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
小众软件
小众软件
F
Fortinet All Blogs
爱范儿
爱范儿
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
C
Check Point Blog
博客园 - 聂微东
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
A
About on SuperTechFans
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
宝玉的分享
宝玉的分享
Jina AI
Jina AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 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