我有一个存储过程,它为前端进行分页并且工作正常。我现在需要修改该过程,将返回的 20 列中的四列分组,然后只返回每个组中包含最低优先级的行。因此,当resort_id、卧室、厨房和入住(日期)都匹配时,只返回具有最低优先级的行。我仍然必须维护分页功能。@startIndex 和 @upperbound 是从前端传递到过程中以进行分页的参数。我认为 RANK OVER (PARTITION BY) 是我无法完全弄清楚如何将它们组合在一起的答案。
SELECT I.id,
I.resort_id,
I.[bedrooms],
I.[kitchen],
I.[checkin],
I.[priority],
I.col_1,
I.col_2 /* ..... (more cols) */
FROM (
SELECT ROW_NUMBER() OVER(ORDER by checkin) AS rowNumber,
*
FROM Inventory
) AS I
WHERE rowNumber >= @startIndex
AND rowNumber < @upperBound
ORDER BY rowNumber
修复后的示例2:
SELECT I.resort_id,
I.[bedrooms],
I.[kitchen],
I.[checkin],
I.[priority],
I.col_1,
I.col_2 /* ..... (more cols) */
FROM Inventory i
JOIN
(
SELECT ROW_NUMBER() OVER(ORDER BY h.checkin) as rowNumber, MIN(h.id) as id
FROM Inventory h
JOIN (
SELECT resort_id, bedrooms, kitchen, checkin, id, MIN(priority) as priority
FROM Inventory
GROUP BY resort_id, bedrooms, kitchen, checkin, id
) h2 on h.resort_id = h2.resort_id and
h.bedrooms = h2.bedrooms and
h.kitchen = h2.kitchen and
h.checkin = h2.checkin and
h.priority = h2.priority
GROUP BY h.resort_id, h.bedrooms, h.kitchen, h.checkin, h.priority
) AS I2
on i.id = i2.id
WHERE rowNumber >= @startIndex
AND rowNumber < @upperBound
ORDER BY rowNumber