Help with ORDER BY clause by string

Asked

Viewed 13 times

0

I have the following select command:

select nome, case grau when 0 then 'QS' when 1 then 'CI' when 2 then 'CDC' when 3 then 'QM' end grau, case faixa when 0 then 'NÃO' when 1 then 'SIM' end faixa, mensalidade from socio order by grau, nome

He’s ordering alphabetically by the GRAU field, but I want him to order in the following sequence:

QM, CDC, CI, QS

How do I make that change?

1 answer

1


You can do it this way:

select nome
    , grau as grau_ordem
    , case grau 
        when 0 then 'QS' 
        when 1 then 'CI' 
        when 2 then 'CDC' 
        when 3 then 'QM' 
        end grau
    , case faixa 
        when 0 then 'NÃO' 
        when 1 then 'SIM' 
        end faixa
    , mensalidade 
from socio 
order by grau_ordem desc
    , nome

So you sort by the desired value (the numeric itself) and have the display column.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.