我有一个查询
SELECT clientcode,
branchcode,
maincode,
name,
actype,
cycode
FROM Master
WHERE name IN (SELECT name
FROM Master
GROUP BY name
HAVING Count(name) > 1)
AND isblocked <> 'C'
AND actype < '4E'
ORDER BY branchcode,
name,
actype,
cycode
现在我需要从上面查询的输出中选择名称相同但ClientCode不同的行。
对于例如,两行具有相同的名称,假设'BISHNU BHANDARI'但是它们应该具有不同的clinetCode,例如'123'用于一个,'444'用于另一个。
答案 0 :(得分:2)
您可以在having
部分添加一个附加条款:
SELECT clientcode,
branchcode,
maincode,
name,
actype,
cycode
FROM Master
WHERE name IN (SELECT name
FROM Master
GROUP BY name
HAVING Count(name) > 1
and min (clientcode) != max (clientcode))
AND isblocked <> 'C'
AND actype < '4E'
ORDER BY branchcode,
name,
actype,
cycode
如果客户端代码可以为空并且您希望null满足不等式条件,则这可能不起作用,但这很容易添加。
答案 1 :(得分:1)
尝试
SELECT min(clientcode), max(clientcode), count(distinct clientcode),
branchcode,
maincode,
name,
actype,
cycode
FROM master
WHERE name IN (SELECT name
FROM master
GROUP BY name
HAVING Count(name) > 1)
AND isblocked <> 'C'
AND actype < '4E'
group by branchcode,
maincode,
name,
actype,
cycode
having 1 < count(distinct clientcode)
order by branchcode,
maincode,
name,
actype,
cycode
如果名称属于不同的分支代码等 你必须从“分组依据”中删除这些字段并选择它们像我上面的客户端代码一样进行聚合