Two months back I had a problem at work with one PostgreSQL queries. Whenever I was using was using GROUP BY I got “column must appear in the GROUP BY clause or be used in an aggregate function” The simple version of a problematic query:

    SELECT "as".* FROM address AS as, customer AS c WHERE as.customer_id = c.id GROUP BY as.id;

Using DISTINCT ON solved this error

    SELECT DISTINCT ON (as.id) "as".* FROM address AS as, customer AS c WHERE as.customer_id = c.id;
Back