GROUP BY and COUNT in PostgreSQL

前端 未结 4 868
小蘑菇
小蘑菇 2020-12-14 00:03

The query:

SELECT COUNT(*) as count_all, 
       posts.id as post_id 
FROM posts 
  INNER JOIN votes ON votes.post_id = posts.id 
GROUP BY posts.id;
<         


        
4条回答
  •  情歌与酒
    2020-12-14 00:35

    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.

提交回复
热议问题