#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Question Generate CASE WHEN Query with ORDER BY involving Secondary Sort


    Below Query I understand is improper wrt syntax. Need to modify query to fetch correct result for me. Please help!

    select * from tableA order by
    case when a is not null then
    a DESC
    else
    b DESC, c DESC END

    Note: DESC or ASC is dynamic coming from java-code.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    Try it like this:
    Code:
    select *
     from  tablea
    order by case when a is not null then 'a desc'
                  else 'b desc, c desc'
             end
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Thanks but not giving correct result.


    This query is sytactically correct but NOT giving correct sorted result.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    Paris Uppland
    Posts
    2,370
    Rep Power
    391
    Code:
    select <columnsOfInterest>
      from tablea
     order
        by case when a is not null then a
                else b end desc, 
        c desc
    If that is not result you desire, you need to supply more details of how you want your data to be ordered.

IMN logo majestic logo threadwatch logo seochat tools logo