Q&A for professional and enthusiast programmers
DECLARE @TotalQuestions AS Int = (SELECT Count(*) FROM Posts WHERE PostTypeId = 1) DECLARE @TotalAnswers AS Int = (SELECT Count(*) FROM Posts WHERE PostTypeId = 2) SELECT CommentCount AS [Number of comments], Count(*) * 100.0 / (@TotalQuestions + @TotalAnswers) AS [% Posts], Count(Nullif(PostTypeId, 2)) * 100.0 / @TotalQuestions AS [% Questions], Count(Nullif(PostTypeId, 1)) * 100.0 / @TotalAnswers AS [% Answers], Count(Nullif(PostTypeId, 2)) AS [# Questions], Count(Nullif(PostTypeId, 1)) AS [# Answers] FROM Posts WHERE (PostTypeId = 1 OR PostTypeId = 2) GROUP BY CommentCount ORDER BY CommentCount ASC