2016-10-21 5 views
1

Kann die folgende Ergebnismenge erreicht werden?wie zwei Tabellen ausgewählt werden und die Daten zusammengeführt werden, um das Duplikat nicht anzuzeigen

tabelle1:

id_s name post_code  city  amount1 
------------------------------------------ 
1  name1 postal1 city1 300 
2  name2 postal2 city2 400 
3  name3 postal3 city3 NULL 
4  name4 postal4 city4 NULL 

table2:

id_p name post_code  city  Amount2 
------------------------------------------ 
1  name1 postal1 city1 300 
2  name2 postal2 city2 NULL 
3  name3 postal3 city3 400 
5  name5 postal5 city5 500 

Ergebnismenge:

id_s name post_code  city  amount1 amount2 
------------------------------------------ 
1  name1 postal1 city1 300   300 
2  name2 postal2 city2 400   NULL 
3  name3 postal3 city3 NULL  400 
4  name4 postal4 city4 NULL  NULL 
5  name5 postal5 city5 NULL  500 
+0

Das ist ein einfaches 'OUTER JOIN' – Amit

+1

Sehr seltsames Tischdesign. Warum speichern Sie so ähnliche Daten in zwei separaten Tabellen? Was ist, wenn gleiche id_p, aber andere Namen/Städte usw.? – jarlh

+0

Können wir Ihrem Problem eine korrekte Lösung anbieten? – rbr94

Antwort

2

Versuchen Sie folgendes:

SELECT 
     COALESCE(table_1.id_s, table_2.id_p) as id 
     ,COALESCE(table_1.name, table_2.name) as name 
     ,COALESCE(table_1.post_code, table_2.post_code) as post_code 
     ,COALESCE(table_1.city, table_2.city) as city 
     ,table1.amount1 
     ,table2.amount2 
FROM table1 
FULL OUTER JOIN table2 ON table_1.id_s = table_2.id_p 

OUTER JOIN: Der FULL OUTER alle Schlüsselwort kehrt die Zeilen aus der linken Tabelle JOIN (Tabelle 1), und alle Zeilen aus der rechten Tabelle (Tabelle 2). Wenn Zeilen in "table1" vorhanden sind, die keine Übereinstimmungen in "table2" aufweisen, oder wenn Zeilen in "table_2" vorhanden sind, für die in "table1" keine Übereinstimmungen vorhanden sind, werden diese Zeilen ebenfalls aufgelistet.

http://www.w3schools.com/sql/sql_join_full.asp

COALESCE: Wertet die Argumente, um und gibt den aktuellen Wert des ersten Ausdrucks, die anfänglich nicht auf NULL werten.

https://msdn.microsoft.com/en-us/library/ms190349.aspx

3

A full outer join die Arbeit machen sollte:

SELECT   COALESCE(id_s, id_p), 
       COALESCE(table1.name, table2.name), 
       COALESCE(table1.post_code, table2.post_code), 
       COALESCE(table1.city, table2.city), 
       amount1, 
       amount2 
FROM   table1 
FULL OUTER JOIN table1 ON id_s = id_p 
4

Try this:

DECLARE @table1 TABLE 
    (id_s INT, name NVARCHAR(20), post_code NVARCHAR(20), 
city NVARCHAR(20), amount1 NVARCHAR(20)) 
    DECLARE @table2 TABLE 
    (id_s INT, name NVARCHAR(20), post_code NVARCHAR(20), 
city NVARCHAR(20), amount2 NVARCHAR(20)) 

    INSERT INTO @table1 VALUES 
    ('1'  ,'name1', 'postal1', 'city1', '300'), 
    ('2'  ,'name2', 'postal2', 'city2', '400'), 
    ('3'  ,'name3', 'postal3', 'city3', NULL), 
    ('4' ,'name4', 'postal4', 'city4', NULL) 

    INSERT INTO @table2 VALUES 
    ('1',  'name1', 'postal1', 'city1', '300'), 
    ('2',  'name2', 'postal2', 'city2', NULL), 
    ('3',  'name3', 'postal3', 'city3', '400'), 
    ('5',  'name5', 'postal5', 'city5', '500') 

    SELECT * FROM @table1 

    SELECT * FROM @table2 

    SELECT 
    ISNULL(t1.id_s,t2.id_s) id_s, ISNULL(t1.name,t2.name) name, 
    ISNULL(t1.post_code,t2.post_code) post_code, ISNULL(t1.city,t2.city) city, 
    amount1,amount2 
    FROM @table1 t1 FULL JOIN @table2 t2 ON t1.id_s = t2.id_s 

Dies wird Ihnen perfekt das Ergebnis Sie suchen geben.

Ich hoffe, es hilft. :)

Verwandte Themen