Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

sql - calculation sum of cost for the full hour

based on the value in the table: timestamp, cost I would like to get the increment sum of cost value every hour.

Table:

Timestamp Price
2021-01-12 00:05:00 1
2021-01-12 01:01:00 1
2021-01-12 02:06:00 3
2021-01-12 03:01:00 3
2021-01-12 03:02:00 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use group by date truncated to hour and then use analytical function as follows:

select timestamp_trunc(Timestamp , HOUR) as tst,
       sum(sum(price)) over (order by timestamp_trunc(Timestamp , HOUR)) as price
  from your_table t
group by tst
order by tst

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...