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?
code4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
I think this may be what you are looking for:
select  /* INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table */

        t1."a"
      , coalesce(t1."b",t2."b") "b"
      , coalesce(t1."c",t2."c") "c"
from Table1 t1
left join Table2 t2 on t1."a" = t2."a"
;

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)
select  /* INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table */

        t1."a"
      , t2."b"
      , t1."c"
from Table1 t1
left join Table2 t2 on t1."a" = t2."a"
;

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
**PostgreSQL 9.3.1 Schema Setup**:

    
    
    CREATE TABLE Table1
    	("a" varchar(1), "b" int, "c" int)
    ;
    	
    INSERT INTO Table1
    	("a", "b", "c")
    VALUES
    	('n', null, 1),
    	('m', null, 3),
    	('o', null, 4)
    ;
    
    CREATE TABLE Table2
    	("a" varchar(1), "b" int, "c" int)
    ;
    	
    INSERT INTO Table2
    	("a", "b", "c")
    VALUES
    	('n', 1, null),
    	('m', 2, null)
    ;

**Query 1**:

    select  /* INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table */
    
            t1."a"
          , coalesce(t1."b",t2."b") "b"
          , coalesce(t1."c",t2."c") "c"
    from Table1 t1
    left join Table2 t2 on t1."a" = t2."a"
    

**[Results][2]**:
    
    | A |      B | C |
    |---|--------|---|
    | m |      2 | 3 |
    | n |      1 | 1 |
    | o | (null) | 4 |


**Query 2**:

    select  /* INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table */
    
            t1."a"
          , t2."b"
          , t1."c"
    from Table1 t1
    left join Table2 t2 on t1."a" = t2."a"
    

**[Results][3]**:
    
    | A |      B | C |
    |---|--------|---|
    | m |      2 | 3 |
    | n |      1 | 1 |
    | o | (null) | 4 |



  [1]: http://sqlfiddle.com/#!15/25921/4

Open in new window

0
earth man2Commented:
Do you want to omit non matching rows in table2 ? If not then use FULL OUTER JOIN instead.
0
awking00Commented:
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?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

code4Author Commented:
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
0
awking00Commented:
Then PortletPaul's solution should work.
0
code4Author Commented:
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.
0
PortletPaulfreelancerCommented:
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
0
earth man2Commented:
SELECT DISTINCT ...
0
earth man2Commented:
I guess if same number of rows is essential then use a correlated subquery.
0
PortletPaulfreelancerCommented:
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...
0
awking00Commented:
Scenarios producing duplicates would include having duplicates in either or both tables. What do the following queries produce (using your data that produced the duplicates)?
select a, b, c, count(*) from table1 group by a, b, c having count(*) > 1;
select a, b, c, count(*) from table2 group by a, b, c having count(*) > 1;
If either or both of these queries return records, then there are duplicates in one or both tables and using distinct would remove them but, if the duplicates are in table1, you will not get one row for each row in that table using the suggested query.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.