Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2005
    Posts
    165

    Unanswered: specify specific sort order....

    I would like to format the output of a query. I have a few CaseCategoryID's I would like to sort; however, I do not want to sort them ascending or descending, I want to sort them by CaseCategoryID 3, then 8, then 4, then 11, and then the rest. Is this possible?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    order
        by case when CaseCategoryID =  3 then 1
                when CaseCategoryID =  8 then 2
                when CaseCategoryID =  4 then 3
                when CaseCategoryID = 11 then 4
                                         else 5 end
         , CaseCategoryID
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    That's the quick solution, but I would recommend you add a column called SortOrder to your CaseCategory table rather than hard-coding.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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