Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

subquery in FROM must have an alias for sql query

I have this code snippet for a query below.

"SELECT DISTINCT(CODE) FROM (" +
    "SELECT TRIM(BOTH ' ' FROM PROJECT_NUMBER) CODE FROM SCHEMA.TABLE_NAME " +
    "WHERE PROJECT_NUMBER IS NOT NULL " +
    "AND LAST_UPDATE_DATE >= :lastUpdateDate " +
"UNION " +
    "SELECT TRIM(BOTH ' ' FROM ANOTHER_CODE) CODE FROM SCHEMA.TABLE_NAME " +
    "WHERE ANOTHER_CODE IS NOT NULL " +
    "AND LAST_UPDATE_DATE >= :lastUpdateDate " +
") " +
"WHERE CODE IS NOT NULL";

I’m in the process of migrating to postgres from oracle and I’m seeing these errors in our logs:

Caused by: org.postgresql.util.PSQLException: ERROR: subquery in FROM must have an alias
  Hint: For example, FROM (SELECT ...) [AS] foo.

Where should the alias go, do I need for all subqueries, and do I need to use the alias in this query? Little confused here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"SELECT DISTINCT(CODE) FROM (" +
    "SELECT TRIM(BOTH ' ' FROM PROJECT_NUMBER) CODE FROM SCHEMA.TABLE_NAME " +
    "WHERE PROJECT_NUMBER IS NOT NULL " +
    "AND LAST_UPDATE_DATE >= :lastUpdateDate " +
"UNION " +
    "SELECT TRIM(BOTH ' ' FROM ANOTHER_CODE) CODE FROM SCHEMA.TABLE_NAME " +
    "WHERE ANOTHER_CODE IS NOT NULL " +
    "AND LAST_UPDATE_DATE >= :lastUpdateDate " +
") as qry " +
"WHERE CODE IS NOT NULL";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading