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
)
------解决方案-------------------- 看上去有点复杂 ------解决方案-------------------- 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 w_name,bm,dw
From tb
where rn in
(
Select max(rn)
From tb
group by w_name
)
--对 w_name 进行分组,查询出w_name对应的集合中 rn 列最大的行 ------解决方案-------------------- delete table where id not in(select w_name, max(ID) from table group by w_name)