Skip to content
Advertisement

SQL sort by each hour with different dates

I have a sql table like this

occ_val     time
0           2/1/2022 3:35:08 pm
1           2/1/2022 3:59:08 pm
2           2/1/2022 4:55:08 pm
3           2/1/2022 5:32:08 pm
2           3/1/2022 4:43:08 pm
3           4/1/2022 2:15:08 pm

How to I sort by each hour to get this:

time            occ_val
2:00:00 pm       3
3:00:00 pm       1
4:00:00 pm       4
5:00:00 pm       3

Your help is very much appreciated 😀

Advertisement

Answer

Here is a MySQL solution:

select DATE_FORMAT(min(time), ‘%h:00’),sum(occ_val) from tbl group by hour(time)

See https://www.db-fiddle.com/f/czudbp5zug6HxX4TFV26GT/0

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement