2012-07-26 80 views
0

我需要在表中選擇所有重複項,但重複項必須通過兩行進行傳播。我嘗試了一些東西,這使我這個代碼:訪問SQL,選擇多行重複

SELECT builder_2, sequence, clientname_2, clientPcode_2, status 
    FROM tblClient 
WHERE clientname_2 IN (SELECT clientname_2 
          FROM tblClient 
         GROUP BY clientPcode_2, clientname_2 
         HAVING (COUNT(clientPcode_2) > 1) AND (COUNT(clientname_2) > 1) 
         ) 
    AND clientPcode_2 IN (SELECT clientPcode_2 
          FROM tblClient 
          GROUP BY clientPcode_2, clientname_2 
         HAVING (COUNT(clientPcode_2) > 1) AND (COUNT(clientname_2) > 1) 
         ) 
ORDER BY clientPcode_2, clientname_2 

但是,此代碼是不工作的原因很明顯,它選擇在這兩個領域的任何副本,因此,如果客戶的名字出現在兩行,他被認爲是即使郵政編碼不匹配也是「重複」。

我試圖做一個單一的子選擇,我會添加主鍵(數字和序列),但結果不是我想要的,似乎是因爲羣組。我無法解釋它,但我的測試數據從樣本中遺漏,因此我拒絕瞭解決方案。

+0

因此,您正在尋找客戶名稱和郵政編碼相同的行,對不對? – 2012-07-26 14:08:01

+0

是的,但正如我在提出的答案中所述,我還需要能夠設置一些過濾器。例如,必須忽略所有具有「拒絕」狀態的客戶名稱+郵政編碼。不僅是具有拒絕狀態的行,而且是與其關聯的副本。謝謝 – Estarius 2012-07-26 14:49:09

回答

2

我認爲像這樣的事情會做你正在尋找的東西。它基本上只是將您的WHERE IN子句移動到更靈活的JOIN

SELECT T1.builder_2, T1.sequence, T1.clientname_2, T1.clientPcode_2, T1.status 
FROM tblClient T1 
     INNER JOIN 
     ( SELECT clientname_2, clientPcode_2 
      FROM tblClient 
      GROUP BY clientname_2, clientPcode_2 
      HAVING COUNT(*) > 1 
     ) AS T2 
      ON T1.clientname_2 = T2.clientname_2 
      AND T1.clientPcode_2 = T2.clientPcode_2 

編輯

我不知道你到底要如何篩選此所以將添加一個例子來說明3種方法中,你可以過濾數據。

樣本數據(僅適用列)

clientname_2 | clientPcode_2 | Status 
----------------+-------------------+-------------------- 
ClientA   | M1 2AA   | Accepted 
ClientA   | M1 2AA   | Pending 
ClientA   | M1 2AA   | Rejected 
ClientB   | M1 2AA   | Accepted 
ClientB   | M1 2AA   | Rejected 
ClientC   | M1 2AA   | Accepted 
ClientC   | M1 2AA   | Pending 



SELECT T1.builder_2, T1.sequence, T1.clientname_2, T1.clientPcode_2, T1.status 
FROM tblClient T1 
     INNER JOIN 
     ( SELECT clientname_2, clientPcode_2 
      FROM tblClient 
      WHERE Status NOT LIKE '%Rejected%'  -- FILTER A 
      GROUP BY clientname_2, clientPcode_2 
      HAVING COUNT(*) > 1 
      AND  SUM(IIF(Status LIKE '%Rejected%', 1, 0)) = 0 -- FILTER C 
     ) AS T2 
      ON T1.clientname_2 = T2.clientname_2 
      AND T1.clientPcode_2 = T2.clientPcode_2 
WHERE Status NOT LIKE '%Rejected%'      -- FILTER B 

濾波器ONLY

這將限制它有2個或更多行,其中的地位不被拒絕只是客戶端/郵政編碼組合。具有拒絕狀態的行仍然是退貨。

clientname_2 | clientPcode_2 | Status 
----------------+-------------------+-------------------- 
ClientA   | M1 2AA   | Accepted 
ClientA   | M1 2AA   | Pending 
ClientA   | M1 2AA   | Rejected 
ClientC   | M1 2AA   | Accepted 
ClientC   | M1 2AA   | Pending 

濾波器B ONLY

這會發現是重複的所有客戶端/郵政編碼組合,並返回只有在狀態不是拒絕行:

clientname_2 | clientPcode_2 | Status 
----------------+-------------------+-------------------- 
ClientA   | M1 2AA   | Accepted 
ClientA   | M1 2AA   | Pending 
ClientB   | M1 2AA   | Accepted 
ClientC   | M1 2AA   | Accepted 
ClientC   | M1 2AA   | Pending 

過濾器和過濾器B

這隻會限制剛纔fi的結果集濾波器一個以除去其中的狀態被拒絕的行

clientname_2 | clientPcode_2 | Status 
----------------+-------------------+-------------------- 
ClientA   | M1 2AA   | Accepted 
ClientA   | M1 2AA   | Pending 
ClientC   | M1 2AA   | Accepted 
ClientC   | M1 2AA   | Pending 

過濾器C

這將只返回在沒有行存在對具有與拒絕狀態相同的組合的客戶端/郵編組合:

clientname_2 | clientPcode_2 | Status 
----------------+-------------------+-------------------- 
ClientC   | M1 2AA   | Accepted 
ClientC   | M1 2AA   | Pending 
+0

這似乎工作,但我不能添加過濾。例如,如果狀態爲「拒絕」,我需要忽略重複項。我可以把它刪除REJECTED行,但不是最初的行。我試圖將它放入INNER JOIN中,但測試數據再次丟失。任何想法如何我可以實現這一點? – Estarius 2012-07-26 14:37:52

+0

@Estarius,你在哪裏添加過濾器?在子查詢或外部查詢中? – nicholas 2012-07-26 15:09:04

+0

我試圖將它添加到內部聯接中,然後在主要選擇 – Estarius 2012-07-26 15:13:11