按时间段统计的SQL

hawkefly 2007-08-03 02:48:49
有这样一个表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的总和,其它时间段不要。
每小时的统计就很好写,像这样的就不知如何写了。
...全文
219 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwq15947619971 2007-08-07
  • 打赏
  • 举报
回复
楼上写的不错
hongqi162 2007-08-07
  • 打赏
  • 举报
回复
--测试数据
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
bobfang 2007-08-07
  • 打赏
  • 举报
回复
是统计每天0点到8点,21点到23这两个时间段各有多少数据吗?如
1日0-8 XXX
1日21-23 XXX
2日0-8 XXX
...

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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