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

推荐订阅源

量子位
I
InfoQ
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
美团技术团队
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
Last Week in AI
Last Week in AI
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LangChain Blog
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
G
Google Developers Blog
博客园 - 叶小钗
博客园 - Franky

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