SQL ORDER BY com CASE com UNION ALL
Execução do PostgreSQL (7.4 e 8.x) e achei que isso estava funcionando, mas agora estou recebendo erro
Eu posso executar as consultas separadamente e funciona muito bem, mas se EU UNION ou UNION ALL, gera um err
Este erro ocorre: (Aviso: pg_query (): consulta falhou: ERRO: a coluna "Campo1" não existe ... PEDIDO POR CASO "Campo1" W ...)
SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName"
WHERE condition
AND other_condition
UNION ALL
SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName"
WHERE yet_another_condition
AND yet_another_other_condition
ORDER BY CASE "Field1"
WHEN 'A' THEN 1
WHEN 'B' THEN 2
WHEN 'C' THEN 3
ELSE 4
END
Isso funciona
SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName"
WHERE yet_another_condition
AND yet_another_other_condition
ORDER BY CASE "Field1"
WHEN 'A' THEN 1
WHEN 'B' THEN 2
WHEN 'C' THEN 3
ELSE 4
END
E isso também funciona:
SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName"
WHERE condition
AND other_condition
ORDER BY CASE "Field1"
WHEN 'A' THEN 1
WHEN 'B' THEN 2
WHEN 'C' THEN 3
ELSE 4
END
e se eu deixar o ORDER BY e usar apenas UNION ou UNION ALL, ele também funciona.
Alguma ideia