通过选择返回的固定行并忽略顶行 SQL 服务器进行分组
Group By selecting fixed rows returned and ignoring the top row SQL Server
我有一个 table:
Customer Purchase
John 5
John 8
John 3
John 1
Sally 3
Sally 5
Sally 2
我想 return 每个客户的两条记录忽略最高购买:
John 5
John 3
Sally 3
Sally 2
具有ROW_NUMBER()
window功能:
select t.customer, t.purchase
from (
select *, row_number() over (partition by customer order by purchase desc) rn
from tablename
) t
where t.rn between 2 and 3