sql重复问题

来源:互联网 发布:算法统宗馒头 编辑:程序博客网 时间:2024/05/29 10:20

create table DistinctOrNot
(id int primary key ,name nvarchar(20))
insert into DistinctOrNot
select 1,'A'
union all
select 2,'B'
union all
select 3,'AB'
union all
select 4,'AB'
union all
select 5,'BC'

select * from DistinctOrNot
---查出重复记录的数据
select [name] from DistinctOrNot
where [name] in (select [name] from DistinctOrNot group by [name] having count(*)>1 )
---过滤重复记录的数据
select * from DistinctOrNot
where id in (select max(id) from DistinctOrNot group by [name])
---删除重复的记录
delete from DistinctOrNot where [name] in (select [name] from DistinctOrNot group by [name] having count(*)>1 )