0
I’m having trouble using Order by com Case on Union, so it works:
select tb_a.nr_rua, tb_a.cod_altura, tb_a.cod_posicao, tb_a.cod_lado where ......
union all
select tb_b.nr_rua, tb_b.cod_altura, tb_b.cod_posicao, tb_b.cod_lado where ......
order by
2,3,1
Now if I do so, using the case:
order by 2,
(case when 4 = 'M' then 3 END) DESC,
(case when 4 <> 'M' then 3 END),
1
Or:
order by 2,
(case when cod_lado = 'M' then cod_posicao END) DESC,
(case when cod_lado <> 'M' then cod_posicao END),
1
Or:
order by 2,
(case when cod_lado = 'M' then 3 END) DESC,
(case when cod_lado <> 'M' then 3 END),
1
Also not working. following Error:
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Invalid command.
invalid ORDER BY clause.
I’m using the Firebird.
If used within select, please check the Firebird https://firebirdsql.org/refdocs/langrefupd15-case.htmldocumentation
– Don't Panic
Right @Everson in my case, which I would have to use to solve my problem?
– Tiago Casanova
select tb_a.nr_rua, tb_a.cod_altura, tb_a.cod_posicao, 
 case 
 when cod_lado = 'M' then 3
 when cod_lado <> 'M' then 4 
 end as cod_lado
where ..... 
union all
select tb_b.nr_rua, tb_b.cod_altura, tb_b.cod_posicao,
 case 
 when cod_lado = 'M' then 3
 when cod_lado <> 'M' then 4 
 end as cod_lado where ......
order by cod_lado
– Don't Panic