Help with ORDER BY clause by string

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

It is sorting alphabetically by the GRADE field, but I want it to sort in the following sequence:

QM, CDC, CI, QS

How do I make this change?

    
asked by anonymous 04.08.2018 / 05:55

1 answer

1

You can do this as follows:

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 numerical override) and have the display column.

    
04.08.2018 / 06:08