Skip to content
Advertisement

GROUP BY and COUNT in PostgreSQL

The query:

Returns n records in Postgresql:

I just want to retrieve the number of records returned: 6.

I used a subquery to achieve what I want, but this doesn’t seem optimum:

How would I get the number of records in this context right in PostgreSQL?

Advertisement

Answer

I think you just need COUNT(DISTINCT post_id) FROM votes.

See “4.2.7. Aggregate Expressions” section in http://www.postgresql.org/docs/current/static/sql-expressions.html.

EDIT: Corrected my careless mistake per Erwin’s comment.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement