sql重複問題

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 )

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章