code4
asked on
sql table merge
I have two tables with the values:
Table 1:
a b c
n - 1
m - 3
o - 4
Table 2:
a b c
n 1 -
m 2 -
I want to create table 3, based on the identity of the string in column "a" between tables 1 and 2
and not omitting any rows of Table 1:
Table 3:
a b c
n 1 1
m 2 3
o - 4
What sql will do this?
Table 1:
a b c
n - 1
m - 3
o - 4
Table 2:
a b c
n 1 -
m 2 -
I want to create table 3, based on the identity of the string in column "a" between tables 1 and 2
and not omitting any rows of Table 1:
Table 3:
a b c
n 1 1
m 2 3
o - 4
What sql will do this?
Do you want to omit non matching rows in table2 ? If not then use FULL OUTER JOIN instead.
Is it possible to have values like?
Table1
n 5 1
m - 3
o - 4
Table2
n 1 -
m 2 -
If so, what would be the expected results for table3?
Table1
n 5 1
m - 3
o - 4
Table2
n 1 -
m 2 -
If so, what would be the expected results for table3?
ASKER
Table 1 never has values in column 2.
I want these values to be filled from the matches in table 2,
and if none are found in table 2, to be left unfilled.
Table 3 should have the same number of rows as table 1.
Thanks
I want these values to be filled from the matches in table 2,
and if none are found in table 2, to be left unfilled.
Table 3 should have the same number of rows as table 1.
Thanks
Then PortletPaul's solution should work.
ASKER
PortletPaul: Thanks, but this did not work.
It created duplicate entries in table 3.
I need table 3 to have exactly the same number of rows as table 1.
It created duplicate entries in table 3.
I need table 3 to have exactly the same number of rows as table 1.
your sample data does not demonstrate this issue,
could you provide more details please?
all we know about your tables is what you put in the question
could you provide more details please?
all we know about your tables is what you put in the question
SELECT DISTINCT ...
I guess if same number of rows is essential then use a correlated subquery.
we might use distinct or maybe a correlated query: who really knows until we see more information
my crystal ball fell off the table and cracked...
my crystal ball fell off the table and cracked...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
but this also matches the expected result above. (i.e. it isn't clear if you want Table1.b to overrule Table2.b and so on)Open in new window
Regarding the select into I'll leave that to you, see:http://www.postgresql.org/docs/9.1/static/sql-selectinto.html
Open in new window