Total Number of Posted Answer and Accepted Answer to a certain user


Please login or register to vote for this query.

(click on this box to dismiss)

Physical Fitness Meta

Q&A about the site for physical fitness professionals, athletes, trainers, and those providing health-related needs

-- This query will calculate the total number of posted answers
--      and the total number of accepted answers to a certain user.

-- It requires three parameters:
--      @UserID               =>  'Your' UserID
--      @MinimumAnswerCount   =>  minimum number of posted answer
--      @MaximumAnswerCount   =>  maximum number of posted answer

-- To counter check if the result is correct, please visit
-- the URL below to get the Links of the question


DECLARE @UserID int = ##UserId##
DECLARE @MinimumAnswerCount int = ##MinimumAnswerCount##
DECLARE @MaximumAnswerCount int = ##MaximumAnswerCount##

SELECT  b.OwnerUserId AS [User Link],
        COUNT(*) PostedAnswer,
        COUNT(CASE WHEN b.AcceptedAnswerId = a.ID THEN 1 END) AS AcceptedAnswer,
            WHEN COUNT(*) = COUNT(CASE WHEN b.AcceptedAnswerId = a.ID THEN 1 END)
            THEN 'Perfect!'
            ELSE ''
        END [Status]
FROM	Posts a
		INNER JOIN Posts b
			ON a.ParentID = b.ID
WHERE   a.PostTypeId = 2 AND
		a.OwnerUserID = @UserID AND
        b.OwnerUserId <> ''
GROUP   BY b.OwnerUserId
HAVING  COUNT(*) BETWEEN @MinimumAnswerCount AND @MaximumAnswerCount
ORDER   BY  PostedAnswer DESC, 
            AcceptedAnswer DESC,
            [User Link] ASC

Enter Parameters

Switch sites:
loading Hold tight while we fetch your results