假设我的输入是:
ID GroupID Qty 1 1 100 2 1 200 3 1 300 4 2 98 5 2 198 6 3 175 7 3 275 8 3 375 9 4 215
输出应该是
ID GroupID Qty 1 1 100 4 2 98 6 3 175 9 4 215
解决方法
declare @T table (ID int,GroupID int,Qty int) insert into @T values (1,1,100),(2,200),(3,300),(4,2,98),(5,198),(6,3,175),(7,275),(8,375),(9,4,215) ;with cte as ( select ID,GroupID,Qty,rank() over(partition by GroupID order by ID) as rn from @T ) select ID,Qty from cte where rn = 1