Oracle按时间统计sql

1.统计某天24小时中,每个小时段的订单数:


select
      count(*),
       to_char(to_date(create_dt, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24')
  from b2c_order_head
   where create_dt like '2014-02-06%'
 group by to_char(to_date(create_dt, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24')
  order by to_char(to_date(create_dt, 'yyyy-mm-dd hh24:mi:ss'),
                  'yyyy-mm-dd hh24')
				  


 

注意:

1.其中create_dt和'yyyy-mm-dd hh24:mi:ss' 必须是对应的

2.select中除了统计字段count(...),其他字段必须与group by的字段一致,

3.order by 总是写到SQL的最后,且分组查询中order by的字段只能是select的字段

4.分组前过滤用where,放到from 后,分组后过滤用having,放到 group by 后

2.统计某天24小时中,个小时段的订单金额:
select sum(order_amt),to_char(to_date(create_dt, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24')
from b2c_order_head
 where create_dt like '2014-02-06%'
 group by to_char(to_date(create_dt, 'yyyy-mm-dd hh24:mi:ss'),
                  'yyyy-mm-dd hh24')
 order by to_char(to_date(create_dt, 'yyyy-mm-dd hh24:mi:ss'),
                  'yyyy-mm-dd hh24')


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值