Benbob
Benbob

Reputation: 14264

Postgres aggregate problem

I'm not sure why this won't work:

SELECT u.id, u.tag, u.unit_type, Count(p.id) AS num_points
FROM ot2.unit u
INNER JOIN ot2.point p on p.unit_id = u.id
GROUP BY u.id
HAVING Count(p.id) > 800;

ERROR: column "u.tag" must appear in the GROUP BY clause or be used in an aggregate function SQL state: 42803

I have already provided a column for group by.

Upvotes: 1

Views: 2794

Answers (1)

Eddy
Eddy

Reputation: 5370

Just what the error says. u.tag is not in the group by and you aren't telling how to aggregate the different u.tag values to a single one.

More concrete you have 2 options:

  • Add u.tag in the group by clause (and u.unit_type as well)
  • use an aggregate function on those 2 fields (min, max, sum, avg, ....)

Upvotes: 2

Related Questions