Sql server 通过选择返回的固定行并忽略SQL Server的顶行来分组
我有一张桌子:Sql server 通过选择返回的固定行并忽略SQL Server的顶行来分组,sql-server,fetch,limit,rows,window-functions,Sql Server,Fetch,Limit,Rows,Window Functions,我有一张桌子: Customer Purchase John 5 John 8 John 3 John 1 Sally 3 Sally 5 Sally 2 我希望每个客户返回两条记录,忽略最重要的购买: John 5 Jo
Customer Purchase
John 5
John 8
John 3
John 1
Sally 3
Sally 5
Sally 2
我希望每个客户返回两条记录,忽略最重要的购买:
John 5
John 3
Sally 3
Sally 2
使用行号()
窗口功能:
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
谢谢你,福帕斯。