gpt4 book ai didi

sql - 在 SQL 中查找 "next but one newest"日期的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-04 19:09:47 25 4
gpt4 key购买 nike

我在 SQL Server 中有一个表,其中包含由 BusinessDataDate 组织的数据。 BusinessDataDate 仅是日期(无时间组件),仅在工作日填充。例如不是周末或公众假期。

因为这个没有具体的顺序,我想找date before the current max date
此查询有效,我可以将值设置为局部变量 - 但感觉必须有更清洁的方法?

SELECT MAX(BusinessDataDate) FROM myTable 
WHERE BusinessDataDate < (SELECT MAX(BusinessDataDate) FROM myTable)

每个日期在表格中都有多行 - 确切的数字是不可预测的。

最佳答案

SELECT TOP (1)
BusinessDataDate
FROM
(
SELECT
BusinessDataDate,
DENSE_RANK() OVER (ORDER BY BusinessDataDate DESC) as rn
FROM
myTable
) x
WHERE
x.rn = 2
ORDER BY
x.BusinessDataDate;

关于sql - 在 SQL 中查找 "next but one newest"日期的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16145544/

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