Skip to content

Instantly share code, notes, and snippets.

View afanasevek's full-sized avatar

afanasevek

View GitHub Profile
SELECT * FROM blog.POSTS p
LEFT JOIN BLOG.POST_VOTES pv
ON p.POST_ID = pv.POST_ID
JOIN (SELECT p2.POST_ID as id, COUNT(pv2.VOTE_ID) "count" FROM BLOG.POSTS p2 LEFT
JOIN BLOG.POST_VOTES pv2 ON p2.POST_ID = pv2.POST_ID GROUP BY p2.POST_ID) r
ON p.POST_ID = r.id ORDER BY r."count";