我正在寻找一种方法来搜索表中的重复值,并将这些重复项(或者甚至只是一组重复项)作为结果集返回。
例如,假设我有这些数据:
uid | semi-unique id 1 | 12345 2 | 21345 3 | 54321 4 | 41235 5 | 12345 6 | 21345
我需要返回:
12345 12345 21345 21345
或者:
12345 21345
我试过谷歌搜索并继续做空。有什么帮助吗?
答案 0 :(得分:3)
SELECT semi_unique_id
FROM your_table
GROUP BY semi_unique_id
HAVING COUNT(semi_unique_id) > 1
如果你想在查询中获取uid,你也可以像这样轻松添加它。
SELECT uid,
semi_unique_uid
FROM your_table
GROUP BY
semi_unique_id,
uid
HAVING COUNT(semi_unique_id) > 1
最后,如果您想了解每行返回多少重复项,您可以执行以下操作。
SELECT uid,
semi_unique_uid,
COUNT(semi_unique_uid) AS unique_id_count
FROM your_table
GROUP BY
semi_unique_id,
uid
HAVING COUNT(semi_unique_id) > 1
答案 1 :(得分:3)
要获取每一行,您可以使用窗口函数:
select t.*
from (select t.*, count(*) over (partition by [semi-unique id]) as totcnt
from t
) t
where totcnt > 1
要获得一个实例,请尝试以下方法:
select t.*
from (select t.*, count(*) over (partition by [semi-unique id]) as totcnt,
row_number() over (partition by [semi-unique id] order by (select NULL)
) as seqnum
from t
) t
where totcnt > 1 and seqnum = 1
这种方法的优点是可以获得所有列,而不仅仅是id(如果有帮助的话)。
答案 2 :(得分:1)
SELECT t.semi_unique_id AS i
FROM TABLE t
GROUP BY
t.semi_unique_id
HAVING (COUNT(t.semi_unique_id) > 1)
尝试使用sql-server