SQL ORDER BY with CASE with UNION ALL SQL ORDER BY with CASE with UNION ALL postgresql postgresql

SQL ORDER BY with CASE with UNION ALL


Put everything in another SELECT:

SELECT * FROM (  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) As AORDER BY CASE field_1    WHEN 'A' THEN 1    WHEN 'B' THEN 2    WHEN 'C' THEN 3    ELSE 4END

or, better, use the alias in ORDER BY, as it is passed at the end of the UNION:

  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 field_1    WHEN 'A' THEN 1    WHEN 'B' THEN 2    WHEN 'C' THEN 3    ELSE 4  END


The first one does not work because you should do

ORDER BY CASE field_1

The "Field1" is only available in a single subquery, and after you make the UNION with a common alias, you cannot refer to that column as "Field1" any more.