Bom dia pessoal,

Existe alguma garantia de que o resultado da query abaixo virá na ordem
que os /union all /estão sendo montados?

Que eu saiba, não.


select *
from (select field from foo where field = 'c'
       union all
       select field from foo where field = 'b'
       union all
       select field from foo where field = 'a') f

Ou seja, vai retornar 'c', 'b', 'a', (nesta ordem), ou isso não é uma
regra? Não encontrei nada na documentação.

Será respeitado o ORDER BY da última consulta, sempre.
O que eu faria é colocar um número de ordenação, por exemplo:

select field
from (select field, 1 as coluna_2 from foo where field = 'c'
       union all
       select field, 2 from foo where field = 'b'
       union all
       select field, 3 from foo where field = 'a') f ORDER BY coluna_2;

Ou faz uma CTE que é mais bonito:

WITH f AS
(select field, 1 as coluna_2 from foo where field = 'c'
union all
select field, 2 from foo where field = 'b'
union all
select field, 3 from foo where field = 'a')
SELECT field FROM f ORDER BY coluna_2;

[]s
Flavio Gurgel
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a