TransWikia.com

Order by -- different sequence for different criteria

Stack Overflow Asked by Donald Li on December 2, 2021

I want to do something like this:

select a
from table
order by
 case when a='A' then b,c,d
 else d,c,b

a, b, c, d are all columns of the the table.

One Answer

Your question is not that clear about the result that you really expect, bu I suspect that is:

order by
    case when a = 'A' then b else d end,
    c,
    case when a = 'A' then d else b end

Or if you want records where a = 'A' first (with the specified order), and then the rest of the records (with the other sequence), then:

order by
    case when a = 'A' then 0 else 1 end,
    case when a = 'A' then b else d end,
    c,
    case when a = 'A' then d else b end

Answered by GMB on December 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP