Case statement in order by

  • I need to order a different way in some cases I thought this would work by it doesn't like the DESC in the case statement:

    ORDER BY result.Cr_Name

    ,CASE WHEN result.Pc_CrFCIC IN (52, 53) THEN result.Ty_Type

    END

    ,result.Pd_UnitNumber

    ,result.Pd_UnitSubNumber

    ,result.Pd_Practice

    ,CASE WHEN result.Pc_CrFCIC IN (52, 53) THEN result.Ip_Code ELSE result.Ip_Code DESC

    END

    ,result.Pd_IsEndorsement

    ,result.Qu_Liability DESC

    Can somebody tell me what I am doing incorrectly? Thank you.

  • Try this:

    CASE WHEN result.Pc_CrFCIC IN (52, 53) THEN result.Ip_Code ELSE 0 END,

    CASE WHEN result.Pc_CrFCIC IN (52, 53) THEN 0 ELSE result.Ip_Code END DESC

    It takes two statements. You can put any value in there where I have a 0. So long as it's a static value, sorting by it won't do anything, which is what you want.

    If it makes more sense, you can try:

    CASE WHEN result.Pc_CrFCIC IN (52, 53) THEN result.Ip_Code ELSE 0 END,

    CASE WHEN result.Pc_CrFCIC NOT IN (52, 53) THEN result.Ip_Code ELSE 0 END DESC

    The only difference is how it reads. Functionally, they should be the same.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • If result.Ip_Code is a number, you could always use it's negative in place of DESC

    CASE WHEN result.Pc_CrFCIC IN (52, 53) THEN result.Ip_Code ELSE -result.Ip_Code END,

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply