Percentage of questions that are unanswered or answered - Part 2 I've refactored my previous query and added better checks when selecting the data. The query link can be found here. The formulas used to calculate the two percentages are the same, and can be seen 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.

DECLARE @TotalQuestions FLOAT = (
  SELECT COUNT(*) FROM Posts WHERE 
        PostTypeId = 1
    AND ClosedDate IS NULL
);

DECLARE @UnansweredQuestions FLOAT = (
  SELECT COUNT(*) 
    FROM Posts AS TopPostsQuery WHERE (
            ClosedDate IS NULL
        AND PostTypeId = 1
        AND AnswerCount = 0
      )
      OR (
            ClosedDate IS NULL
        AND PostTypeId = 1
        AND AnswerCount &lt;= 1
        AND AcceptedAnswerId IS NULL
        AND (
          SELECT Score FROM Posts AS SubPostsQuery WHERE
                PostTypeId &lt;&gt; 1
            AND ParentId = TopPostsQuery.Id
        ) &lt;= 0
      )
);

DECLARE @AnsweredPercentage FLOAT =
  100.0 - ((@UnansweredQuestions / @TotalQuestions) * 100.0);

DECLARE @UnansweredPercentage FLOAT = 
  (@UnansweredQuestions / @TotalQuestions) * 100.0;

SELECT
    [Percentage Of Answered Questions]   = @AnsweredPercentage
  , [Percentage Of Unanswered Questions] = @UnansweredPercentage;
Your code has certainly improved from last time, however there's a few things that could be improved:


AND AnswerCount &lt;= 1: that could be AND AnswerCount &lt; 0.
You don't need to recreate the amount of unanswered questions twice:



DECLARE @AnsweredPercentage FLOAT =
  100.0 - ((@UnansweredQuestions / @TotalQuestions) * 100.0);

DECLARE @UnansweredPercentage FLOAT = 
  (@UnansweredQuestions / @TotalQuestions) * 100.0;



into:

DECLARE @UnansweredPercentage FLOAT = 
  (@UnansweredQuestions / @TotalQuestions) * 100.0;

DECLARE @AnsweredPercentage FLOAT =
  100.0 - @UnansweredPercentage;