删除表table1中重复记录,只保留一条记录。
一、指某两条或多条记录的所有字段值均相同,包括主键ID也相同。二、指某两条或多条记录的其中两个字段A、B相同,主键ID为INT自增型。就这两题,分别用SQL语句做,谢谢大家了,最好有详细的解释,本人是菜鸟~~
一、指某两条或多条记录的所有字段值均相同,包括主键ID也相同。二、指某两条或多条记录的其中两个字段A、B相同,主键ID为INT自增型。就这两题,分别用SQL语句做,谢谢大家了,最好有详细的解释,本人是菜鸟~~
既然ID是主键了,怎么可能还会重复呢?
declare @table1 table(ID int identity(1,1) primary key,A int,B int,C int)
insert into @table1
select 1,2,3 union all
select 2,2,3 union all
select 1,3,3 union all
select 1,2,3 union all
select 1,2,3 union all
select 3,2,1 union all
select 1,2,3 union all
select 1,3,3 union all
select 4,2,3 union all
select 1,4,3delete @table1
from @table1 a
inner join (select row_number() over(partition by A,B order by ID) rn,* from @table1) b
on a.ID=b.ID and a.A=b.A and a.B=b.B and b.rn>1select * from @table1/*
ID A B C
----------- ----------- ----------- -----------
1 1 2 3
2 2 2 3
3 1 3 3
6 3 2 1
9 4 2 3
10 1 4 3
*/