2016-05-02 15 views
0

Ich habe eine vorhandene Überprüfung, die in der Name-Tabelle nach doppelten Namen (Full_Name) sucht, aber wie kann ich nach Name und Adresse suchen? Full_Address lebt in der Adreßtabelle und wenn ich versuche, diese zwei Werte zu kombinieren, um gegen die DB als ein einzelner Wert zu prüfen, bricht alles zusammen.Überprüfen von zwei Werten aus zwei Tabellen für Duplikate

Select Name.ID, Name.Full_Name, Concat(Name.Full_Name,' ', Address.FULL_ADDRESS) as Comb 
FROM Name INNER JOIN Address ON Name.ID = Address.ID 
Where Full_Name != '' AND having count(Comb)>1 
group by Full_Name 

Antwort

1
DECLARE @Name TABLE (ID INT, Full_Name NVARCHAR(50)) 
DECLARE @Address TABLE (ID INT, FULL_ADDRESS NVARCHAR(100)) 

INSERT INTO @Name VALUES 
    (1,'Alex Zoolittle') 
    ,(2,'Brian Yakami') 
    ,(3,'Charles Xylogon') 
    ,(4,'Brian Yakami') 

INSERT INTO @Address VALUES 
    (1,'123 Westwood Way, Los Angeles, CA 95043') 
    ,(2,'234 Eastwood Lane, Los Gatos, CA 95030') 
    ,(3,'345 Northwood Blvd, Los Alamos, NM 83241') 
    ,(4,'234 Eastwood Lane, Los Gatos, CA 95030') 

;WITH Comb 
    AS (
     SELECT na.ID, na.Full_Name, CONCAT(na.Full_Name,' ', ad.FULL_ADDRESS) AS Comb, 
      ROW_NUMBER() OVER(PARTITION BY CONCAT(na.Full_Name,' ', ad.FULL_ADDRESS) ORDER BY na.ID) AS Row 
      FROM @Name na 
       INNER JOIN @Address ad 
        ON na.ID = ad.ID 
     WHERE Full_Name != '' 
     ) 

SELECT ID, Full_Name, Comb FROM Comb 
    WHERE Row = 1 
Verwandte Themen