/**//* 删除表中重复行的方法 */ ifexists(select1from sys.tables where name='t_dup') droptable t_dup go createtable t_dup(id int,age int,name varchar(32)) insertinto t_dup select1,1,'a' unionallselect1,1,'a' unionallselect1,1,'a' unionallselect1,2,'a' unionallselect1,2,'e' unionallselect2,3,'b' unionallselect3,3,'d' go select*from t_dup go with t_all as (select id,age,name,row_number() over(orderby id) [num] from t_dup), t_max as (select id,age,name,max(num) num from t_all groupby id,age,name) select id,name,age from t_all a whereexists(select1from t_max where num=a.num)
以前写的东西,可以看看思路 /**//*删除表中重复行的方法*/if exists(select 1 from sys.tables where name=t_dup) drop table t_dupgocreate table t_dup(id int,age int,name varchar(32))insert into t_dupselect 1,1,auni