|
SQL Server2005中删除表中重复行的方法:
if exists(select 1 from sys.tables where name='t_dup')
drop table t_dup
go
create table t_dup(id int,age int,name varchar(32))
insert into t_dup
select 1,1,'a'
union all select 1,1,'a'
union all select 1,1,'a'
union all select 1,2,'a'
union all select 1,2,'e'
union all select 2,3,'b'
union all select 3,3,'d'
go
select * from t_dup
go
with t_all as
(select id,age,name,row_number() over(order by id) [num]
from t_dup),
t_max as
(select id,age,name,max(num) num
from t_all
group by id,age,name)
select id,name,age from t_all a where exists(select 1 from t_max where num=a.num) 文中只是表达去除重复的思路,并且在实际操作中,可能需要只针对部分组合列要求唯一。比如读取时id,age组合相同的,只取其中一条记录,同时再给表增加一个列,这种情况,通常就需要借助于行号完成了。那就不能直接goup by 来分组输出需要的结果了。
| |