我想生成从CustomersHub
中的最大日期到其他日期(例如'2022-11-16'
)的日期范围。
在where子句中使用标量列会导致查询似乎永远运行。我检查了停止条件以确保查询不会无限运行,并且为了测试,我输入了一个常量值,该值基本上与表列的值相同。查询开始运行良好。
下面是两个表:
create table #Temp
(
dt DateTime,
);
create table CustomersHub
(
id int,
firstloadedDate DateTime,
);
下面是我如何将数据插入到临时表中:
insert into #Temp
select top 1 hub.firstloadedDate max_date from CustomersHub hub order by max_date desc;
最后查询生成日期范围:
WITH e00(n)
AS (SELECT 1
UNION ALL
SELECT 1),
e02(n)
AS (SELECT 1
FROM [e00] [a],
[e00] [b]),
e04(n)
AS (SELECT 1
FROM [e02] [a],
[e02] [b]),
e08(n)
AS (SELECT 1
FROM [e04] [a],
[e04] [b]),
e16(n)
AS (SELECT 1
FROM [e08] [a],
[e08] [b]),
e32(n)
AS (SELECT 1
FROM [e16] [a],
[e16] [b]),
num_tally(n)
AS (SELECT Row_number()
OVER (
ORDER BY ( SELECT NULL) )
FROM [e32]),
tally
AS (SELECT Dateadd(day, n - 1, dt) dates,
n,
dt
FROM [num_tally],
#temp
WHERE Datediff(day, dt, '2022-11-16') >= n)
SELECT *
FROM tally
DROP TABLE #temp
在tally
cte中,如果我用'2022-10-20'
这样的日期替换dt
,那么它就可以工作,否则它就一直运行。
1条答案
按热度按时间lhcgjxsq1#
相反,我会生成一个包含足够多数字的Tally,然后使用
DATEADD
:db<>fiddle