Users with more than 10 questions and exactly 1 answer ordered by Question average score

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

SELECT 
    users.Id as 'UserId', 
    users.DisplayName as 'UserDisplayName', 
    users.Reputation as 'UserReputation', 
    users.LastAccessDate as 'UserLastAccessDate',
    questions.QuestionsCount,
    CAST(questions.AverageQuestionScore AS DECIMAL(10,2)) as 'AverageQuestionScore', 
    answers.AnswersCount, 
    CAST(answers.AverageAnswerScore AS DECIMAL(10,2)) as 'AverageAnswerScore', 
    CAST((answers.AnswersCount / CAST(questions.QuestionsCount AS DECIMAL(10,2))) AS DECIMAL(10,2)) as 'AnswerToQuestionRatio'

FROM Users users
    JOIN (
        SELECT 
            u.Id as "UserId", 
            COUNT(*) as "QuestionsCount", 
            AVG(CAST(Score AS DECIMAL(10, 2))) as "AverageQuestionScore"
        FROM Posts p
            JOIN USERS u
                ON p.OwnerUserId = u.Id
        WHERE p.PostTypeId = 1
        GROUP BY u.Id
    ) questions

        ON users.Id = questions.UserId

    JOIN (
        SELECT 
            u.Id as "UserId", 
            COUNT(*) as "AnswersCount", 
            AVG(CAST(Score AS DECIMAL(10, 2))) as "AverageAnswerScore"
        FROM Posts p
            JOIN USERS u
                ON p.OwnerUserId = u.Id
        WHERE p.PostTypeId = 2
        GROUP BY u.Id
    ) answers

        ON users.Id = answers.UserId
WHERE 
    questions.QuestionsCount > 10 AND 
    answers.AnswersCount = 1
ORDER BY AverageQuestionScore

Enter Parameters

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