我需要在表格中选择所有重复项,但重复必须分布在两行中。我尝试了一些引导我使用此代码的东西:
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
但是,由于显而易见的原因,此代码无法正常工作,它会在两个字段中选择任何重复项,因此如果客户端名称出现在两行中,则会将其视为"重复"即使邮政编码不匹配。
我尝试做一个单子选择,我会添加主键(数字和序列),但结果不是我想要的,似乎是因为group by。我无法解释,但样本中缺少我的测试数据,所以我拒绝了解决方案。
答案 0 :(得分: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
仅限过滤
这将仅限于具有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
仅限过滤器
这将找到所有重复的客户端/邮政编码组合,并仅返回状态未被拒绝的行:
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
过滤器A和过滤器B
这将仅限制过滤器a的结果集以删除状态被拒绝的行
clientname_2 | clientPcode_2 | Status
----------------+-------------------+--------------------
ClientA | M1 2AA | Accepted
ClientA | M1 2AA | Pending
ClientC | M1 2AA | Accepted
ClientC | M1 2AA | Pending
FILTER C
这将仅返回客户端/邮政编码组合,其中不存在具有相同组合且状态为已拒绝的行:
clientname_2 | clientPcode_2 | Status
----------------+-------------------+--------------------
ClientC | M1 2AA | Accepted
ClientC | M1 2AA | Pending