我有一个大表,其中包含有关用户的以下数据。
social security number
name
address
我想在表格中找到所有可能的重复项 ssn相等但名称不是
的地方我的尝试是:
SELECT * FROM Table t1
WHERE (SELECT count(*) from Table t2 where t1.name <> t2.name) > 1
答案 0 :(得分:14)
SSN上的分组应该这样做
<击> 撞击>
<击>SELECT
ssn
FROM
Table t1
GROUP BY
ssn
HAVING COUNT(*) > 1
..或者每个ssn有很多行,只想找到重复的名称)
...
HAVING COUNT(DISTINCT name) > 1
击> <击> 撞击>
编辑,哎呀,误解
SELECT
ssn
FROM
Table t1
GROUP BY
ssn
HAVING MIN(name) <> MAX(name)
答案 1 :(得分:0)
这将处理两个以上重复ssn的记录:
select count(*), name from table t1, (
select count(*) ssn_count, ssn
from table
group by ssn
having count(*) > 1
) t2
where t1.ssn = t2.ssn
group by t1.name
having count(*) <> t2.ssn_count