Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:07:42+00:00 2026-05-23T07:07:42+00:00

Running PostgreSQL (7.4 and 8.x) and I thought this was working but now I’m

  • 0

Running PostgreSQL (7.4 and 8.x) and I thought this was working but now I’m getting errors.

I can run the queries separately and it works just fine, but if I UNION or UNION ALL it throws an error.

This errors out: (Warning: pg_query(): Query failed: ERROR: column “Field1” does not exist … ORDER BY CASE “Field1” 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

This works:

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

And this works as well:

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

and if I leave off the ORDER BY and just use the UNION or UNION ALL it works as well.

Any Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:07:43+00:00Added an answer on May 23, 2026 at 7:07 am

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

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Final update: I had forgotten to run the initdb command. By running this command
I have imported a PostgreSQL database and I keep getting this error: ERROR: operator
I'm running a Postgresql query and getting the following error: ActiveRecord::StatementInvalid (PGError: ERROR: could
I'm running PostgreSQL on mt Windows 7 machine. To run a database I type:
I'm running: PostgreSQL 9.0.3 (Last Debian stable) This query: SELECT * FROM sis.thread_categories stc,
All, I am running CentOS 6.0 with Postgresql 8.4 and can't seem to figure
I have a really big database (running on PostgreSQL) containing a lot of tables
I have a Rails 2.0.2 application running with a postgresql db. The machine will
I'm running a query against a table in a postgresql database. The database is
Is there a way to disable the Postgresql translation of messages? I´m running my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.