Q&A for professional and enthusiast programmers
/* Upvoted and downvotes for answers per question */ ;WITH QuestionsSample AS ( SELECT q.Id FROM Posts q tablesample(5 percent) WHERE q.PostTypeId = 1 AND q.Id > 0 AND q.AnswerCount > 0 ) SELECT q.Id as QuestionId, a.Id as AnswerId, v.VoteTypeId, COUNT(a.Id) as n, q.Id as [Post Link] FROM Posts a RIGHT OUTER JOIN QuestionsSample q ON a.ParentId = q.Id AND a.Id > 0 LEFT OUTER JOIN Votes v ON a.Id = v.PostId AND (v.VoteTypeId = 2 OR v.VoteTypeId = 3) AND a.PostTypeId = 2 GROUP BY q.Id, a.Id, v.VoteTypeId ORDER BY q.Id, a.Id, v.VoteTypeId