I am currently getting all data for yesterday from my database but I want to get data for each hour. This is my query:
select
camera_id,
count(*) as snapshot_count
from snapshots
where created_at >= TIMESTAMP 'yesterday'
and created_at < TIMESTAMP 'today'
group by camera_id
It's giving me all records for yesterday, but how could it be possible to get records from yesterday but for each hour?
PS: I want to run this in one query, not 24 queries
Best Answer
Use
date_part
to get the hour part of your timestamp, then add it to yourGROUP BY
clause.I generated an example table:
and populated it with some test data:
After adding an ordering clause, here's the first few rows of output: