PostgreSQL - GROUP BY timestamp values? PostgreSQL - GROUP BY timestamp values? postgresql postgresql

PostgreSQL - GROUP BY timestamp values?


This does the trick without the date_trunc function (easier to read).

// 2014select created_on::DATE from users group by created_on::DATE// updated September 2018 (thanks to @wegry)select created_on::DATE as co from users group by co

What we're doing here is casting the original value into a DATE rendering the time data in this value inconsequential.


Grouping by a timestamp column works fine for me here, keeping in mind that even a 1-microsecond difference will prevent two rows from being grouped together.

To group by larger time periods, group by an expression on the timestamp column that returns an appropriately truncated value. date_trunc can be useful here, as can to_char.