Retrieve Top n Records Per Group

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

XSL transform from string to string

Example code to do XSL transform from string to string

XPathDocument doc = new XPathDocument(new StringReader(inputXML));
XslTransform xslt = new XslTransform();
xslt.Load(whatever);
StringWriter sw = new StringWriter();
xslt.Transform(doc, null, sw);

The resulting string is in sw.ToString().