SQL ORDER BY con CASE con UNION ALL
Running PostgreSQL (7.4 y 8.x) y pensé que esto estaba funcionando, pero ahora recibo errores.
Puedo ejecutar las consultas por separado y funciona bien, pero si UNION o UNION ALL arroja un error.
Este error se soluciona: (Advertencia: pg_query (): Error en la consulta: ERROR: la columna "Campo1" no existe ... ORDEN 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
Esto 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
Y esto también 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
y si dejo el ORDER BY y solo uso la UNION o UNION ALL, también funciona.
¿Algunas ideas