我認爲像這樣的事情會做你正在尋找的東西。它基本上只是將您的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
因此,您正在尋找客戶名稱和郵政編碼相同的行,對不對? – 2012-07-26 14:08:01
是的,但正如我在提出的答案中所述,我還需要能夠設置一些過濾器。例如,必須忽略所有具有「拒絕」狀態的客戶名稱+郵政編碼。不僅是具有拒絕狀態的行,而且是與其關聯的副本。謝謝 – Estarius 2012-07-26 14:49:09