Monday, April 2, 2007

Sorting once, sorting twice...

We came across a situation in which we had to sort a table (limited by a WHERE clause) and order this again by another column. We thought this was easy to do using a subselect but SQL Server refuses to sort views. After some experimenting we came up with the following query:

SELECT * FROM
(
SELECT TOP 5 topicKnowledgeID, topicID, rating, tblTopicKnowledge.date, username
FROM dbo.tblTopicKnowledge
WHERE (topicID = @topicID AND username = @username)
ORDER BY topicKnowledgeID DESC
) AS sortedTopicKnowledgeTable
ORDER BY topicKnowledgeID ASC


Great succes!!

No comments: