Skip to content

ORDER BY with CASE doesn't work on UNION #2416

@4skinSkywalker

Description

@4skinSkywalker

I'm trying this query

SELECT country, COUNT(*) AS competitors
FROM foreignCompetitors
GROUP BY country

UNION

SELECT "Total: " AS country, COUNT(*) AS competitors
FROM foreignCompetitors

ORDER BY 
    CASE
        WHEN country = "France" THEN 1
        WHEN country = "Germany" THEN 2
        WHEN country = "USA" THEN 3
        WHEN country = "UK" THEN 4
        ELSE 5
    END;

But the ORDER BY clause isn't doing anything, as seen here:

Image

But if I remove the CASE and just put the ORDER BY country it works, as seen here:

Image

I can confirm that the CASE normally works in the ORDER BY clause, here an example query:

SELECT country, COUNT(*) AS competitors
FROM foreignCompetitors
GROUP BY country
ORDER BY 
    CASE
        WHEN country = "France" THEN 1
        WHEN country = "Germany" THEN 2
        WHEN country = "USA" THEN 3
        WHEN country = "UK" THEN 4
        ELSE 5
    END;

Results:

Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions