sql-server - 通过选择返回的固定行并忽略顶行 SQL Server 进行分组
问题描述
我有一张桌子:
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
推荐阅读
- javascript - 在 JavaScript 中将 JSON 对象转换为基于索引的 JSON 数组
- aws-lambda - 如何设置依赖于使用 SAM 创建的另一个无服务器函数的 ARN 的 SAM 调用权限?
- gremlin - 在任意 Gremlin 查询中捕获所有访问节点的路径
- excel - 如何选择条件格式突出显示的单元格?
- mysql - 如何创建共享数据库池中间件?
- model-view-controller - 将日历绑定到 mvc 文本框
- hadoop - 无法理解蜂巢中外部关键字的意义
- python - Python获取在数据框中出现最多的类别对
- jsf - 使用 Primefaces 滤镜后的问题
- javascript - vue中按div ID汇总值