我使用以下查询来查找重复记录
SELECT F.ID,f.CIVILID
FROM DBO.EMPUDF AS F
WHERE f.civilid IN (SELECT CIVILID
FROM dBO.EMPUDF
GROUP BY CIVILID
HAVING COUNT (CIVILID) > 1)
子查询返回218条记录,这些记录是重复记录,但是当我尝试执行查询以返回name和id时,它会返回超过10k条记录,其中包含许多空id列。
答案 0 :(得分:0)
嗯,你正在处理重复项,所以它们有很多。
你可以通过直方图的直方图来了解有多少是重复的:
SELECT cnt, COUNT(*), MIN(CIVILID), MAX(CIVILID)
FROM (SELECT CIVILID, COUNT(*) as cnt
FROM dBO.EMPUDF
GROUP BY CIVILID
) c
GROUP BY cnt
ORDER BY cnt;
此外,您可以使用窗口函数更简单地编写原始查询:
SELECT e.*
FROM (SELECT e.*, COUNT(*) OVER (PARTITION BY CIVILID) as cnt
FROM dBO.EMPUDF
) e
WHERE cnt > 1;