'SQL Query for items in the last 24 hours grouping by hour

I have user workloads I want to query for a chart. For every entry there is a timestamp with the usage.

I have the query:

SELECT TIME(usage_timestamp), usage FROM usages WHERE usage_timestamp > date_sub(now(), INTERVAL 1 DAY);

for every entry in the last 24 Hours.

If I want now the average usage for the last 24 hours - grouped by hour, I just get the usage from the last day:

SELECT TIME(usage_timestamp) as timecode, AVG(usage) as usages FROM usages WHERE usage_timestamp > date_sub(now(), INTERVAL 1 DAY) GROUP BY HOUR(usage_timestamp);

Can someone help me here please?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source