Skip to content
Advertisement

Postgresql count query

I have table with this structure

I would like to perform a single sql query to get tags related to a specific job and its occurrence among ALL jobs. Right now I have such query which apparently does not work SELECT tag, count(tag) as tagcount FROM job_tags WHERE job_id=$1 GROUP BY tagcount ORDER BY tagcount DESC. It may return only one as tagcount. If anyone knows how to build this query properly I would be very happy! Thank you

Advertisement

Answer

I would recommend group by, and filtering with a having clause. Postgres’ boolean aggregation functions come handy for this:

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