Number of answers, accepted answers and accepted percent

1

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

-- Number of answers, accepted answers and accepted percent 

DECLARE @UserId int = ##UserId##

SELECT 
    (SELECT Count(*) FROM Posts WHERE OwnerUserId = @UserId AND PostTypeId = 2) AS TotalAnswers,
    Count(a.Id) AS AcceptedAnswers, 
    (CAST(Count(a.Id) AS float) / (SELECT Count(*) FROM Posts WHERE OwnerUserId = @UserId AND PostTypeId = 2) * 100) AS AcceptedPercentage
FROM Posts q INNER JOIN Posts a ON q.AcceptedAnswerId = a.Id
WHERE a.OwnerUserId = ##UserId##
AND a.PostTypeId = 2

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results