对于重复行,如何只保留重复行中的一行?例如....
对于重复行,如何只保留重复行中的一行?例如....
有数据表wzbm有w_name,bm,dw三列,已知w_name有重复的行
想用SELECT distinct w_name,bm,dw FROM wzbm 排除重复行后导入新的表中,用这样的方法来清除重复行中多余的,但不行,用 distinct w_name 可以,但后面再加别的列就不行了。
如何能做到对于w_name有重复的行,只保留一行?
或者筛选出来都是唯一的行后,再导入新表也行
[解决办法]
select *
from wzbm t
where not exists (select 1 from wzbm where w_name = t.w_name and bm > t.bm)
Use tempdb
GO
Create table dbo.wzbm
(w_name varchar(10),bm int,dw int )
Go
Insert Into dbo.wzbm values('a',1,2),('a',2,3),('b',2,3)
Go
with tb as
(
Select *,ROW_NUMBER() Over (Order by w_name) as rn
From dbo.wzbm
)
Select t1.w_name,t1.bm,t1.dw
From tb as t1
Where not exists
(Select *
From tb as t2
where t2.rn > t1.rn And t1.w_name = t2.w_name
)