Extract accepted and most-voted answer of a question

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 Id AS [Post Link], high, highest
FROM (
    SELECT TOP 1 Id, Body as high, Score as highest, DENSE_RANK() OVER(ORDER BY Score DESC) Rank 
    FROM (
        SELECT p.Id, p.ParentId, p.Body, p.Score
        FROM Posts p 
        WHERE p.ParentId = ##QuestionId##
    )x
    ORDER BY Rank ASC
)x

UNION
SELECT Id AS [Post Link], low, lowest
FROM (
    SELECT TOP 1 Id, Body as low, Score as lowest, DENSE_RANK() OVER(ORDER BY Score DESC) Rank 
    FROM (
        SELECT p.Id, p.ParentId, p.Body, p.Score
        FROM Posts p 
        WHERE p.ParentId = ##QuestionId##
    )x
    ORDER BY Rank DESC
)x

Enter Parameters

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