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], Body, Score as highest
FROM (
    SELECT TOP 1 Id, Body, Score, 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], Body, Score as lowest
FROM (
    SELECT TOP 1 Id, Body, Score, 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