我有这样的数据
dt | price
---------------------------
2016-02-02 12:33:33 3.3
2016-03-02 12:33:33 3.3
2016-02-02 12:03:33 3.3
2016-02-02 12:50:33 3.3
2016-02-02 13:33:33 3.3
我需要按小时分组的总和价格,但我需要包括年,月和日部分
理想的结果:
2016-02-02 12:00:00 9.9
2016-02-02 13:00:00 3.3
2016-03-02 12:00:00 3.3
我有查询,它给出了这个结果。问题是,可能有更有效的方法来做到这一点吗?
我当前的查询:
SELECT TO_TIMESTAMP(d, 'YYYY-MM-DD HH24:MI:SS') AS hours, price FROM (
SELECT substr(dt, 1, 13) AS d, sum(price) as price FROM table
GROUP BY substr(dt, 1, 13)
)
ORDER BY hours
答案 0 :(得分:3)
试试这个
select
TRUNC(dt,'HH24'),
sum(price)
from table
group by TRUNC(dt,'HH24')
order by 2 desc;
答案 1 :(得分:1)
使用to_char:
select to_char(dt, 'YYYY-MM-DD HH24')||':00:00' as HourBlock, sum(price) as TotalPrice
from MyTable
group by to_char(dt, 'YYYY-MM-DD HH24')||':00:00'