Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用递归CTE查找特定日期之前的最大值_Sql_Sql Server_Recursion_Common Table Expression - Fatal编程技术网

Sql 使用递归CTE查找特定日期之前的最大值

Sql 使用递归CTE查找特定日期之前的最大值,sql,sql-server,recursion,common-table-expression,Sql,Sql Server,Recursion,Common Table Expression,考虑下表(称之为表1): 。。。 等等 如何在一条语句中编写一个T-SQL命令,用于返回所有小于或等于日期列中日期的日期和结果列的最大值?换句话说,期望的结果输出将是: 1/1/2020 10 1/2/2020 10 1/3/2020 12 1/4/2020 13 1/5/2020 13 1/6/2020 13 我在考虑递归CTE,但我似乎不知道如何编写 提前感谢您的时间和投入。您不会为此使用递归CTE。您将使用窗口函数: selec

考虑下表(称之为表1):

。。。 等等

如何在一条语句中编写一个T-SQL命令,用于返回所有小于或等于日期列中日期的日期和结果列的最大值?换句话说,期望的结果输出将是:

1/1/2020      10
1/2/2020      10
1/3/2020      12
1/4/2020      13
1/5/2020      13
1/6/2020      13
我在考虑递归CTE,但我似乎不知道如何编写


提前感谢您的时间和投入。

您不会为此使用递归CTE。您将使用窗口函数:

select t.*, max(result) over (order by date) as max_to_date
from t;
select t.*, max(result) over (order by date) as max_to_date
from t;