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')