我有一个表。
Customer Purchase
John 5
John 8
John 3
John 1
Sally 3
Sally 5
Sally 2
我想为每个客户返回两条记录 忽略最上面的购买。
John 5
John 3
Sally 3
Sally 2
用 ROW_NUMBER()
窗口功能。
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