How to Group by Time

In order to group by time, we need to define the granularity level of the time element to group by. For example if we define a group by hour, then we need to extract the hour from the timestamp, and group by this derived field.

select extract(hour from sold_at) as hour, 
    sum(revenue) as total_revenue
from   sales
group by extract(hour from sold_at)
order by 1
hour total_revenue
11 1530.00
12 1910.00

IN THIS PAGE