如表中有id 、name字段,数据分别为
1 张三
2 李四
3 李四
4 王五
5 李四
现在我想实现
(1)去除重复的数据,使得表中重复的数据就剩1条(把3条一样的数据除去,使得只剩1条李四),也就是表变
1 张三
2 李四
3 王五
(2)统计出重复的数据的 count
谢了
1 张三
2 李四
3 李四
4 王五
5 李四
现在我想实现
(1)去除重复的数据,使得表中重复的数据就剩1条(把3条一样的数据除去,使得只剩1条李四),也就是表变
1 张三
2 李四
3 王五
(2)统计出重复的数据的 count
谢了
2. select name,count(1) from T group by name having count(1)>1
select name,count(*) as 重复的次数 from tb group by name
还有一个,如果我想删除重复的数据,使得只剩下1条
谢谢了
这样写的话,会全部删除掉那些重复的数据,但我只想保留一条呢,其他的2条我要删除掉,如果是显示就是
select name from T group by name
create table #a
(
id int
, name varchar(20)
)
insert into #a
select 1,'张三' union all
select 2,'李四' union all
select 3,'李四' union all
select 4,'王五' union all
select 5,'李四' --1:
select
distinct min(id) as id
, name
from #a
group by name
order by min(id)
结果:
id name
----------- --------------------
1 张三
2 李四
4 王五(3 行受影响)--2:
select name,count(1)as 重复数 from #a
group by name
having count(1) >1结果:
name 重复数
-------------------- -----------
李四 3(1 行受影响)
select 1,'张三' union all
select 2,'李四' union all
select 3,'李四' union all
select 4,'王五' union all
select 5,'李四'select distinct min(id)as id,[name] from #a
group by [name]select [name],count([name]) as '总数' from #a
group by [name]
having count([name])>1
select name,count(1) from T group by name having count(1)>1
2.但是到底应该删除哪些,保留哪一条,是保留最小id的那一条?
所以必须先把存在重复记录的名字插入一个临时表,然后取得这些名字记录中id最小的一条update一个字段。以此为标记。删除未置位该标记的其他行。