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:
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