Case no order by

1

I'm trying to make an order by the result of sql:

sql.... 
order by cod_rua,
 case cod_lado
     when 'M' then cod_lado desc
     else cod_lado ASC
 end, cod_altura

Only you are giving error in desc.

Staff did not work:

order by
 r.cod_altura, r.nr_rua,
 (case when tb_confcamara.cod_lado =  'M' then r.cod_posicao) desc,
 (case when tb_confcamara.cod_lado <> 'M' then r.cod_posicao)

Next Error:

Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 21, column 60.
)

I'm using Firebird.

    
asked by anonymous 04.05.2017 / 19:18

2 answers

2

Separate your columns correctly with the help of case . It is important to note that there can be no ambiguity in the conditions.

Each column (the cases ) will receive the keyword DESC or ASC .

Note: Entering the keyword ASC is optional.

sql.... 
order by cod_rua,
         (case when cod_lado = 'M' then cod_lado) desc,
         (case when cod_lado <> 'M' then cod_lado),
         cod_altura
    
04.05.2017 / 19:38
1
order by cod_rua,
 (case cod_lado
     when 'M' then cod_lado 
     else cod_lado ASC
 end) desc, 
cod_altura

The "case" is a column the "desc" comes after.

    
04.05.2017 / 19:30