create table Student(
ID varchar(10) not null,
Name varchar(10) not null,
);
insert into Student values('1', 'zhangs');
insert into Student values('2', 'zhangs');
insert into Student values('3', 'lisi');
insert into Student values('4', 'lisi');
insert into Student values('5', 'wangwu');
刪除Name重復多余的行,每個Name僅保留1行數據
1、查詢表中Name 重復的數據
select Name from Student group by Name having count(Name) > 1
2、有唯一列,通過唯一列最大或最小方式刪除重復記錄
檢查表中是否有主鍵或者唯一值的列,當前可以數據看到ID是唯一的,可以通過Name分組排除掉ID最大或最小的行
delete from Student where Name in( select Name from Student group by Name having count(Name) > 1) and ID not in(select max(ID) from Student group by Name having count(Name) > 1 )
執行刪除腳本后查詢
3、無唯一列使用ROW_NUMBER()函數刪除重復記錄
如果表中沒有唯一值的列,可以通過 來刪除重復數據
重復執行插入腳本,查看表數據,表中沒有唯一列值
Delete T From (Select Row_Number() Over(Partition By [Name] order By [ID]) As RowNumber,* From Student)T Where T.RowNumber > 1
小知識點
語法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
表示根據COLUMN分組,在分組內部根據 COLUMN排序,而此函數計算的值就表示每組內部排序后的順序編號(組內連續的唯一的)
函數“Row_Number”必須有 OVER 子句。OVER 子句必須有包含 ORDER BY
Row_Number() Over(Partition By [Name] order By [ID]) 表示已name列分組,在每組內以ID列進行升序排序,每組內返回一個唯一的序號
執行刪除腳本后查詢表數據