gpt4 book ai didi

sql-server - T-SQL : WHERE clause in OVER(PARTITION BY. .. 排序依据...)

转载 作者:行者123 更新时间:2023-12-02 12:02:06 25 4
gpt4 key购买 nike

我有以下查询

;WITH tmp AS
(
SELECT *, ROW_NUMBER()
OVER
(PARTITION BY to_tel, duration, call_date
ORDER BY rates_start DESC) as rn
FROM ##TempTable
)
SELECT *
FROM tmp
WHERE rn = 1
ORDER BY customer_id, to_code, duration

但我想修改它,它不给我最大 rates_start,而是给我特定日期之前的最大 rates_start。有什么办法可以做到这一点吗?

最佳答案

您可以在 cte 部分中添加 WHERE 。我不确定在这种情况下您是否仍想按 call_date 进行分区(我将其删除)。如果需要,请更改 PARTITION BY 部分。

;WITH tmp AS
(
SELECT *, ROW_NUMBER()
OVER
(PARTITION BY to_tel, duration
ORDER BY rates_start DESC) as rn
FROM ##TempTable
WHERE call_date < @somedate
)
SELECT *
FROM tmp
WHERE rn = 1
ORDER BY customer_id, to_code, duration

关于sql-server - T-SQL : WHERE clause in OVER(PARTITION BY. .. 排序依据...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com