...

View Full Version : MS-SQL, Sorting Question



rynox
05-11-2005, 05:13 PM
I'm sure there has to be a way to do this, but I've never found anything in the online docs. Here is just an example of what I want to do:
SELECT TOP 3 * FROM requests That will output something like this:
STATUS TITLE REQUESTOR
-------------------------------------------
H SOME TITLE HERE AUDREY HEPBURN
P ANOTHER TITLE CARRY GRANT
N SOMETHING HERE DORRIS DAY
Now what I want to do is sort them based on the STATUS column, but I want it in a particular order: N > H > P

Obviously, my real-life scenario is more complex and involves a UNION of 3 different queries, but if someone could show me by example, it would be a huge help.

Thanks.

rynox

rynox
05-11-2005, 10:32 PM
Answered my own question. In case anyone is interested:
SELECT TOP 3 * FROM requests
ORDER BY
CASE STATUS
WHEN 'N' THEN 1
WHEN 'H' THEN 2
WHEN 'P' THEN 3
ELSE 4
END
Would yield:
STATUS TITLE REQUESTOR
-------------------------------------------
N SOMETHING HERE DORRIS DAY
H SOME TITLE HERE AUDREY HEPBURN
P ANOTHER TITLE CARRY GRANT



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum