Q&A for peer programmer code reviews
WITH RankedQuestions AS ( SELECT Id, CAST(ViewCount AS BIGINT) AS ViewCount, ROW_NUMBER() OVER (ORDER BY ViewCount DESC) AS row_num, COUNT(*) OVER () AS total_questions FROM Posts WHERE PostTypeId = 1 -- 1 represents questions in the Posts table ), Top10Percent AS ( SELECT SUM(CAST(ViewCount AS FLOAT)) AS top_10_views FROM RankedQuestions WHERE row_num <= CEILING(0.1 * total_questions) ), TotalViews AS ( SELECT SUM(CAST(ViewCount AS FLOAT)) AS total_views FROM Posts WHERE PostTypeId = 1 -- 1 represents questions in the Posts table ) SELECT (top_10_views * 100.0 / NULLIF(total_views, 0)) AS top_10_percent_view_ratio FROM Top10Percent, TotalViews;