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 datetime, and group by this derived field.

select datepart(hour, sales_timestamp) as hour, sum(revenue) as total_revenue
from sales
group by datepart(hour, sales_timestamp)
hour total_revenue
12 15.14
11 55.51