varshadusa01
New Contributor

PostgreSQL: Take last 3 hours and group by 5 minutes [duplicate]

I was wondering if you can help me write a query that should just SELECT count(*) but only include data from last 3 hours and group it by 5 minutes.

So I have a table that has a createdts so I have the date there. I just want to see how many entries I have in the last 3 hours, but group COUNT(*) per 5 minutes.

SELECTCOUNT(*)FROMmytableWHEREcreatedts>=now()::日期-interval'3 hour'GROUPBY'every 5 minutes'

Also, what's really important is that theCount(*)'sthat are null, get defaulted to 0. I have many windows of time where the data will be null, and having it default to0saves a lot of headache later

Visit: postgresql course

1 REPLY1
varshadusa01
New Contributor