Retrieve Top n Records Per Group
SELECT
DiscussionTopPosts.PostID
From (
SELECT p.DiscussionID, p.PostID, ROW_NUMBER() OVER (PARTITION BY p.DiscussionID ORDER BY p.DateCreated DESC) AS RowNumber
FROM Posts p
WHERE
p.DiscussionID IN
(
SELECT d.DiscussionID
FROM Discussions d
Where myCondition
)
AND p.IsActive = 1
GROUP BY p.DiscussionID, p.PostID, p.DateCreated
)
DiscussionTopPosts
WHERE
DiscussionTopPosts.RowNumber <= @NumberOfPosts
Labels: SQL Code Library
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment