如何统计 一天24小时 每个小时的数据总数啊 sql怎么写 求指点 谢谢

cqwish16 2015-03-03 10:42:42
如何统计 一天24小时 每个小时的数据总数啊 sql怎么写 求指点 谢谢
时间 条数​
1 33
2 2
3 98
............
24 11
...全文
12657 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
碧水幽幽泉 2017-09-13
  • 打赏
  • 举报
回复
引用 9 楼 u010438126 的回复:


--创建测试表
--循环插入测试数据
declare n number;
begin
n:=0;
while n<50000 loop
n :=n+1;
insert into hour_test
select * from (
select rownum rn,to_date('2017-01-01', 'yyyy-mm-dd hh:mi:ss')+ rownum/24 add_hour from dual
connect by rownum<25) a
where a.rn =(select round(dbms_random.value(1,24)) from dual);
end loop;
commit;
end ;
--查询结果
select to_char(add_hour,'hh24') hour ,count(*) n from hour_test
group by to_char(add_hour,'hh24')
order by to_char(add_hour,'hh24');


这个写法可以
qq_40191645 2017-09-11
  • 打赏
  • 举报
回复
引用 11 楼 weixin_40097177 的回复:
为什么不导出到excel表 一个sum语句就解决了?
和你的意思一样需要在excel表中体现,要用sum算总和,具体的图在上面,能写下sql吗?在线等,谢谢
qq_40191645 2017-09-11
  • 打赏
  • 举报
回复
引用 11 楼 weixin_40097177 的回复:
为什么不导出到excel表 一个sum语句就解决了?

weixin_40097177 2017-09-07
  • 打赏
  • 举报
回复
不好意思,我理解错了!
weixin_40097177 2017-09-07
  • 打赏
  • 举报
回复
为什么不导出到excel表 一个sum语句就解决了?
  • 打赏
  • 举报
回复
http://blog.csdn.net/qq_28766327/article/details/77864672 我的解决办法
  • 打赏
  • 举报
回复


--创建测试表
--循环插入测试数据
declare n number;
begin
n:=0;
while n<50000 loop
n :=n+1;
insert into hour_test
select * from (
select rownum rn,to_date('2017-01-01', 'yyyy-mm-dd hh:mi:ss')+ rownum/24 add_hour from dual
connect by rownum<25) a
where a.rn =(select round(dbms_random.value(1,24)) from dual);
end loop;
commit;
end ;
--查询结果
select to_char(add_hour,'hh24') hour ,count(*) n from hour_test
group by to_char(add_hour,'hh24')
order by to_char(add_hour,'hh24');

qq_30190057 2017-08-09
  • 打赏
  • 举报
回复
楼主 能否把你这个问题给我解答一下 小弟碰到几天了 没点头绪 能否加我QQ 1628288336
cqwish16 2015-03-03
  • 打赏
  • 举报
回复
引用 4 楼 IceIsabel 的回复:
select trunc(createTime),to_char(createTime,'hh24'),count(*) from orderList group by trunc(createTime),to_char(createTime,'hh24');
不好意思可能是我没说清楚,你这样是查询所有的数据 我只要查一天之类的数据, 并且把为空的 也查询出来 比如2点 0 3点0.你这样查询 数据为空的 查询不出来 谢谢
IceIsabel 2015-03-03
  • 打赏
  • 举报
回复
select trunc(createTime),to_char(createTime,'hh24'),count(*) from orderList group by trunc(createTime),to_char(createTime,'hh24');
cqwish16 2015-03-03
  • 打赏
  • 举报
回复
引用 1 楼 cqwish16 的回复:
表 a 里面有个时间字段 ORDERTIME
表名 orderList 字段 createTime name ....... 2014-02-14 12:11:22 test
  • 打赏
  • 举报
回复
把表结构和样例数据贴出来吧
cqwish16 2015-03-03
  • 打赏
  • 举报
回复
表 a 里面有个时间字段 ORDERTIME
我看见佛 2015-03-03
  • 打赏
  • 举报
回复
create table orderList(
createTime date default(sysdate),
name varchar2(20)              
)
----------------------------------------
insert into orderList (name) values ('aa');
insert into orderList  (name) values ('bb');
insert into orderList (name) values  ('cc');
insert into orderList (name) values  ('dd');
insert into orderList  (name) values ('ee');
insert into orderList  (name) values ('ff');
----------------------------------------
with C_tab as(
select to_char(sysdate,'yyyy-mm-dd')||' '||lpad(level,2,'0') atime from dual connect by level <=24)
select b.atime,count(nvl(a.createtime,null)) from orderList a right join C_tab b on to_char(a.createtime,'yyyy-mm-dd hh24') = b.atime
group by b.atime
----------------------------------------
drop table orderList;
新年,我的写法。
IceIsabel 2015-03-03
  • 打赏
  • 举报
回复
引用 5 楼 cqwish16 的回复:
[quote=引用 4 楼 IceIsabel 的回复:] select trunc(createTime),to_char(createTime,'hh24'),count(*) from orderList group by trunc(createTime),to_char(createTime,'hh24');
不好意思可能是我没说清楚,你这样是查询所有的数据 我只要查一天之类的数据, 并且把为空的 也查询出来 比如2点 0 3点0.你这样查询 数据为空的 查询不出来 谢谢[/quote] 要查一天的就加上where条件,至于数据为空的也要查出来的话可以考虑建一个表,存00~23这些值,左连接一下

17,086

社区成员

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

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