patternsqlMinor
Percentage of questions that are unanswered or answered
Viewed 0 times
unansweredarequestionsthatansweredpercentage
Problem
I put together this simple query which calculates the total percentage of answered questions and unanswered questions. The formulas used to calculate these are shown below:
$$
\text{Answered Percentage} = 100 - \left(\frac{\text{Unanswered}}{\text{Answered}} \times 100\right) \\
\text{Unanswered Percentage} = \frac{\text{Unanswered}}{\text{Answered}} \times 100
$$
Finally, here's the source:
$$
\text{Answered Percentage} = 100 - \left(\frac{\text{Unanswered}}{\text{Answered}} \times 100\right) \\
\text{Unanswered Percentage} = \frac{\text{Unanswered}}{\text{Answered}} \times 100
$$
Finally, here's the source:
DECLARE @TotalQuestions DECIMAL = (
SELECT COUNT(*) FROM Posts
);
DECLARE @UnansweredQuestions DECIMAL = (
SELECT COUNT(*) FROM Posts WHERE AnswerCount = 0
);
-- The percentage of un-answered questions. Calculated
-- using this:
-- (Unanswered Questions / Total Site Questions) * 100
SELECT
(@UnansweredQuestions / @TotalQuestions) * 100.0
AS [Percentage Of Unanswered Questions];
-- The percentage of answered questions. Calculated
-- using this:
-- 100 - ((Unanswered Questions / Total Site Questions) * 100)
SELECT
100 - ((@UnansweredQuestions / @TotalQuestions) * 100.0)
AS [Perentage Of Answered Questions];Solution
Comments
Your comments are mostly clutter, and I mean that because your naming is good enough to where the comments are mostly irrelevant. Also, if you do need to make multi-line comment, you can use the same syntax in SQL as in pretty much everything else.
Look at this, the comments and the code read almost identically:
There is an elegant way of assigning an alias in an expression that would make it even easier to read:
Does exactly the same thing, but reads more like typical code, where you name a thing before assigning it something, rather than after.
Dead code
Your first
Columns:
Rows:
Your comments are mostly clutter, and I mean that because your naming is good enough to where the comments are mostly irrelevant. Also, if you do need to make multi-line comment, you can use the same syntax in SQL as in pretty much everything else.
Look at this, the comments and the code read almost identically:
/**
* The percentage of un-answered questions. Calculated
* using this:
* (Unanswered Questions / Total Site Questions) * 100
*/
SELECT
(@UnansweredQuestions / @TotalQuestions) * 100.0 AS [Percentage Of Unanswered Questions];There is an elegant way of assigning an alias in an expression that would make it even easier to read:
SELECT
[Percentage Of Unanswered Questions] = (@UnansweredQuestions / @TotalQuestions) * 100.0Does exactly the same thing, but reads more like typical code, where you name a thing before assigning it something, rather than after.
Dead code
Your first
SELECT query is never displayed. There are two ways you could fix that, either by making them two different columns, or two different rows (according to your needs).Columns:
SELECT
[Percentage Of Unanswered Questions] = (@UnansweredQuestions / @TotalQuestions) * 100.0
, [Perentage Of Answered Questions] = 100 - ((@UnansweredQuestions / @TotalQuestions) * 100.0);Rows:
SELECT
[Type of value] = 'Percentage Of Unanswered Questions'
, [Value] = (@UnansweredQuestions / @TotalQuestions) * 100.0
UNION
SELECT
'Percentage Of Answered Questions'
, 100 - ((@UnansweredQuestions / @TotalQuestions) * 100.0);Code Snippets
/**
* The percentage of un-answered questions. Calculated
* using this:
* (Unanswered Questions / Total Site Questions) * 100
*/
SELECT
(@UnansweredQuestions / @TotalQuestions) * 100.0 AS [Percentage Of Unanswered Questions];SELECT
[Percentage Of Unanswered Questions] = (@UnansweredQuestions / @TotalQuestions) * 100.0SELECT
[Percentage Of Unanswered Questions] = (@UnansweredQuestions / @TotalQuestions) * 100.0
, [Perentage Of Answered Questions] = 100 - ((@UnansweredQuestions / @TotalQuestions) * 100.0);SELECT
[Type of value] = 'Percentage Of Unanswered Questions'
, [Value] = (@UnansweredQuestions / @TotalQuestions) * 100.0
UNION
SELECT
'Percentage Of Answered Questions'
, 100 - ((@UnansweredQuestions / @TotalQuestions) * 100.0);Context
StackExchange Code Review Q#96277, answer score: 7
Revisions (0)
No revisions yet.