Use PostgreSQL-compatible group by statement