Question

I have a table that looks like:

Table name Demo

id         creation_date
1          2021-03-06 12:27:48.811
2          2021-03-06 03:56:48.875
3          2021-03-06 03:58:23.567

I need the count of records hourly wise in PostgreSql.

Was it helpful?

Solution

Postgres can truncate a date to a certain precision:

SELECT DATE_TRUNC('hour', creation_date) FROM demo

So all you have to do is group and count it:

SELECT DATE_TRUNC('hour', creation_date), COUNT(*) 
FROM demo 
GROUP BY DATE_TRUNC('hour', creation_date)
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top