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

Save output table from LEFT JOIN

I want to left join two tables, but I cannot save the resulting table.

When I use the following code, I get the desired result:

SELECT *
FROM tableA a
LEFT JOIN tableB b ON a.ID = b.ID AND a.Name = b.Name

However, when I want to save the result in a new table and use:

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

SELECT *
INTO new_table
FROM tableA a
LEFT JOIN tableB b ON a.ID = b.ID AND a.Name = b.Name

I get the following error

Column names in each table must be unique

Based on other questions, I tried addressing this issue the following way:

SELECT a.ID, a.Name
INTO new_table
FROM tableA a
LEFT JOIN tableB b ON a.ID = b.ID AND a.Name = b.Name

However, now the resulting table only contains columns ID and Name. And none of the columns from tableB.

Any help is highly appreciated.

>Solution :

You could alias the columns from A (you really only -need- to alias the columns that need to be unique, but you can optionally assign aliases to the rest) or B using the AS keyword to make them unique and then add all the columns from B

SELECT a.ID AS A_ID,
       a.Name AS A_Name,
       b.*
INTO new_table
FROM tableA a
LEFT JOIN tableB b ON a.ID = b.ID and a.Name = b.Name
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