利用row_number函数处理重复数据

 
利用 row_number 函数处理重复数据
 
-- 创建测试表 person
CREATE TABLE person(
    sn varchar(10),
    name varchar(50)
);
go
 
-- 插入测试重复数据
insert into person values('1','guoqiang1');
insert into person values('1','guoqiang1');
insert into person values('1','guoqiang1');
insert into person values('2','guoqiang2');
insert into person values('2','guoqiang2');
insert into person values('3','guoqiang3');
go
 
-- 查看 person
select * from dbo.person;
go
 
-- 去除重复数据,并放到 tmp 表中
select sn,name into tmp from
( select *, row_number() over(partition by sn order by sn) as rank from person) as t
where t.rank = 1
go
 
-- 查看 tmp
select * from dbo.tmp
go
 
 

你可能感兴趣的:(SQL,Server,商业智能)