使用sql server 怎么查重复数据
发布网友
发布时间:2022-04-07 16:58
我来回答
共3个回答
热心网友
时间:2022-04-07 18:28
1、最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:
select name from emp group by name having count(*)>1
所有名字重复人的记录是:
select * from emp
where name in (select name from emp group by name having count(*)>1)
2、稍微再聪明一点,就会想到,如果对每个名字都和原表进行比较,大于2个人名字与这条记录相同的就是合格的 ,就有:
select * from emp
where (select count(*) from emp e where e.name=emp.name) >1
热心网友
时间:2022-04-07 19:46
列出表中某字段有重复的记录,每条记录后面带重复次数。
例如:
id name
--------
1 aaa
2 bbb
3 ccc
4 bbb
5 aaa
6 aaa
7 ccc
8 ddd
9 eee
查询结果应如下:
id name repeat
-----------------
1 aaa 3
5 aaa 3
6 aaa 3
2 bbb 2
4 bbb 2
3 ccc 2
7 ccc 2
第一种解决方案:
SELECT A.id, A.name, B.repeat
FROM Table_1 AS A INNER JOIN
(SELECT name, COUNT(*) AS repeat
FROM Table_1
GROUP BY name) AS B ON A.name = B.name
WHERE (B.repeat > 1)
ORDER BY A.name, A.id
第二种解决方案:
SELECT name, COUNT(*) AS repeat
FROM Table_1
GROUP BY name
HAVING (COUNT(*) > 1)
我觉得你应该把表和字段列出来,这样别人的回答可以更明晰化.
哪几个字段或者哪个字段要查重的?
参考资料:http://hi.baidu.com/kissjava/blog/item/3e637df4f4f28cdbf2d385f6.html
热心网友
时间:2022-04-07 21:20
SELECT *,
count(*) AS CCC
FROM 表明
GROUP BY 字段
ORDER BY CCC DESC