Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2012
    Posts
    6

    Unanswered: conditional Order by in SQL server 2008

    In SQL sERVER 2008, I have two fields - Depatment and Employees. I need to sort the result set by employee number ascending order, with following exception

    1) when department number = 50 - the preferred order is Employee # - 573 followed by 551-572 (employee # belong to Dept 50 = 551-573)

    2) When Department number = 20 – the preferred sort order is Employee # 213-220, followed by Employee # 201-213 (employee # belong to Dept 20 = 201-220)

    How shall I achieve this? Please help

  2. #2
    Join Date
    Apr 2012
    Posts
    213
    Try:

    Code:
    select * from MyTable
    order by 
        Departament,
        case when (Department = 50 and Employee = 573) or
                  (Department = 20 and Employee between 213 and 220)
            then 1 else 2 
        end,
        Employee
    Hope this helps.

  3. #3
    Join Date
    Aug 2004
    Location
    Dallas, Texas
    Posts
    831
    Since when are we doing people's homework for them? :-(

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    this be not homework, honets

    that sort requirement is so wacky, only a real-life phb could've come up with it
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Aug 2004
    Location
    Dallas, Texas
    Posts
    831
    Either that or college became surreal.

Tags for this Thread

Posting Permissions

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