Q&A about the site for mechanics and DIY enthusiast owners of cars, trucks, and motorcycles
SELECT TOP 1 p.Id as [Post Link], SUM(CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE 0 END) AS Upvotes, SUM(CASE WHEN v.VoteTypeId = 3 THEN 1 ELSE 0 END) AS Downvotes, SUM(CASE WHEN v.VoteTypeId IN (2, 3) THEN 1 ELSE 0 END) AS TotalVotes FROM Posts p INNER JOIN Votes v ON p.Id = v.PostId WHERE p.PostTypeId = 1 -- 1 represents questions GROUP BY p.Id ORDER BY TotalVotes DESC;