Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MS-SQL, Sorting Question

    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:
    Code:
    SELECT TOP 3 * FROM requests
    That will output something like this:
    Code:
    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

  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Answered my own question. In case anyone is interested:
    Code:
    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:
    Code:
    STATUS  TITLE            REQUESTOR
    -------------------------------------------
    N       SOMETHING HERE   DORRIS DAY
    H       SOME TITLE HERE  AUDREY HEPBURN
    P       ANOTHER TITLE    CARRY GRANT


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •