Case no order by

Asked

Viewed 2,102 times

1

I am trying to make an order by as 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’re making a mistake on the way down.

Guys didn’t work out:

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)

Following Error:

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

I am using Firebird.

  • If cod_lado for = ’M' you want to sort the descending column, if not increasing?

  • Thiago, is reply of SO-En will help you, I just don’t know how it applies in Banco Firebird.

2 answers

2


Correctly separate your columns with the help of the 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: Enter 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

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.

Browser other questions tagged

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