How To Query For Values Which Have The Highest Vote Count And No Flags In PostgreSQL?
I have a database structure that's sort of like this: nodes id properties id node_id name assignments id property_id value votes id assignment_id flags id
Solution 1:
You can try the below query -
SELECT P.name, A.value, V.CNT_VOTES total_votes
FROM properties P
INNER JOIN assignments A ON P.id = A.property_id
INNER JOIN (SELECT assignment_id, COUNT(*) CNT_VOTES
FROM votes
GROUP BY assignment_id) V ON V.assignment_id = A.id
LEFT JOIN flags F ON F.assignment_id = A.id
AND F.assignment_id IS NULL
ORDER BY V.CNT_VOTES DESC
LIMIT 1;
Post a Comment for "How To Query For Values Which Have The Highest Vote Count And No Flags In PostgreSQL?"