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:
Using DISTINCT ON solved this error