2013-03-05 40 views
2

我需要爲具有重複的多部分標識符選擇某些字段。選擇帶有多部分標識符的內連接並具有子句

我有查詢搶重複正確的。

SELECT b.MemEmpID, b.LastName, b.FirstName 
       FROM  table1 As b 
       GROUP BY b.MemEmpID, b.LastName, b.FirstName 
       HAVING Count(*) > 1 

但找到重複後,我需要從同一個表,但僅適用於具有重複的多標識符攫取更多的信息。所以如下所示。

有人可以幫我用正確的語法呢?

SELECT a.memempid, a.depkey, a.lastname, a.firstname, a.birthdate, a.memrelation 
FROM table1 As a 
INNER JOIN(SELECT b.MemEmpID, b.LastName, b.FirstName 
       FROM  table1 As b 
       GROUP BY b.MemEmpID, b.LastName, b.FirstName 
       HAVING Count(*) > 1) 
ON b.memempid = a.memempid 
AND b.lastname = a.lastname 
AND b.firstname = a.firstname 

回答

2

你真的很接近。您需要別名子查詢。

table1 As b失去它的範圍與分組()所以你需要別名加括號的子查詢。您可以重複使用b - 它只會知道返回的列,但爲了清晰起見,我選擇了(...) as c來消除歧義。

SELECT a.memempid, a.depkey, a.lastname, a.firstname, a.birthdate, a.memrelation 
FROM table1 As a 
INNER JOIN 
    (SELECT b.MemEmpID, b.LastName, b.FirstName 
    FROM  table1 As b 
    GROUP BY b.MemEmpID, b.LastName, b.FirstName 
    HAVING Count(*) > 1 
    ) as c 
ON c.memempid = a.memempid 
    AND c.lastname = a.lastname 
    AND c.firstname = a.firstname 
+0

非常感謝你!!!!我正在嘗試每一次,但這完美的工作! – user2136054 2013-03-05 15:13:10

相關問題