在Azure Synapse Analytics中,递归添加日期而不是最大递归。

huangapple go评论67阅读模式
英文:

Recursively add dates in Azure Synapse Analytics instead of Max Recursion

问题

在表MonthCalender中,我已添加了两个日期范围,并尝试在范围内插入每月的开始日期和月末日期。

|      月份名称        |      月初日期          | 月末日期                |
|---------------------|-----------------------|-----------------------|
|   以前的月份          |2010-01-01 00:00:00.000|2023-06-01 08:21:30.813|
|   更新数值            |2023-06-01 08:21:30.817|2030-01-01 00:00:00.000|

我想要像这样插入每月的开始日期和月末日期:

|      月份名称        |      月初日期          | 月末日期                |
|---------------------|-----------------------|-----------------------|
|   以前的月份          |2010-01-01 00:00:00.000|2010-02-01 00:00:00.000|
|   以前的月份          |2010-02-01 00:00:00.000|2010-03-01 00:00:00.000|
|   以前的月份          |2010-03-01 00:00:00.000|2010-04-01 00:00:00.000|
|   以前的月份          |2010-04-01 00:00:00.000|2010-05-01 00:00:00.000|
|   以前的月份          |2010-05-01 00:00:00.000|2010-06-01 00:00:00.000|
|   以前的月份          |2010-06-01 00:00:00.000|2010-07-01 00:00:00.000|

... 直到 2023-06-01 00:00:00.000

代码中使用了maxrecursion,但在synapse中不受支持。我该如何使用循环在范围内插入数据?

;WITH cSequence AS (     
SELECT  '以前的月份' as 月份名称, 
  Cast('2010-01-01 00:00:00.000' as datetime) AS 开始范围,  
  Cast(DATEADD(m, 1, '2010-01-01 00:00:00.000') as datetime) AS 结束范围
UNION ALL     
SELECT  CASE WHEN 结束范围 >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) THEN '更新数值' ELSE '以前的月份' END as 月份名称, 
  结束范围,					 
  DATEADD(m, 1, 结束范围)
FROM cSequence      
WHERE DATEADD(m, 1, 结束范围) < '2030-01-01 00:00:00.000' )  
insert into #MonthCalendar 
SELECT * FROM cSequence 
OPTION (MAXRECURSION 0); 
英文:

In the table MonthCalender, I have added two date ranges and I am trying to insert monthly start and month end dates within the range.

MonthName MonthStart MonthEnd
Previous Months 2010-01-01 00:00:00.000 2023-06-01 08:21:30.813
UpdateValue 2023-06-01 08:21:30.817 2030-01-01 00:00:00.000

I want to insert month start and month end dates like this.

MonthName MonthStart MonthEnd
Previous Months 2010-01-01 00:00:00.000 2010-02-01 00:00:00.000
Previous Months 2010-02-01 00:00:00.000 2010-03-01 00:00:00.000
Previous Months 2010-03-01 00:00:00.000 2010-04-01 00:00:00.000
Previous Months 2010-04-01 00:00:00.000 2010-05-01 00:00:00.000
Previous Months 2010-05-01 00:00:00.000 2010-06-01 00:00:00.000
Previous Months 2010-06-01 00:00:00.000 2010-07-01 00:00:00.000

... till 2023-06-01 00:00:00.000

The Code is using maxrecursion but that is not supported in synapse. How do I use a loop to insert data within the range?

;WITH cSequence AS (     
SELECT  &#39;Previous Months&#39; as MonthName, 
  Cast(&#39;2010-01-01 00:00:00.000&#39; as datetime) AS StartRange,  
  Cast(DATEADD(m, 1, &#39;2010-01-01 00:00:00.000&#39;) as datetime) AS EndRange
UNION ALL     
SELECT  CASE WHEN EndRange &gt;= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) THEN &#39;UpdateValue&#39; ELSE &#39;Previous Months&#39; END as MonthName, 
  EndRange,					 
  DATEADD(m, 1, EndRange)
FROM cSequence      
WHERE DATEADD(m, 1, EndRange) &lt; &#39;2030-01-01 00:00:00.000&#39; )  
insert into #MonthCalendar 
SELECT * FROM cSequence 
OPTION (MAXRECURSION 0); 

答案1

得分: 1

在您的情况下,我了解到不支持递归或MAXRECURSION选项的使用,因此您可以使用循环在所需的日期范围内插入数据。

DECLARE @StartRange DATETIME = '2010-01-01 00:00:00.000'
DECLARE @EndRange DATETIME = '2023-06-01 00:00:00.000'

WHILE @StartRange < @EndRange
BEGIN
    DECLARE @NextMonthStart DATETIME = DATEADD(MONTH, 1, @StartRange)
    INSERT INTO #MonthCalendar (MonthName, MonthStart, MonthEnd)
    VALUES ('前几个月', @StartRange, @NextMonthStart)

    SET @StartRange = @NextMonthStart
END

循环将持续,直到@StartRange的值超过@EndRange的值。在每次迭代中,它将通过将1个月添加到当前的@StartRange值来计算下个月的起始日期。

然后,它将为#MonthCalendar表插入一行,其中包含适当的值,包括MonthNameMonthStartMonthEnd。最后,它将更新@StartRange的值为计算得到的@NextMonthStart值,以进行下一次迭代。

英文:

In your case I understood that the use of recursion or MAXRECURSION option is not supported, so you can use a loop to insert data within the desired date range.

DECLARE @StartRange DATETIME = &#39;2010-01-01 00:00:00.000&#39;
DECLARE @EndRange DATETIME = &#39;2023-06-01 00:00:00.000&#39;

WHILE @StartRange &lt; @EndRange
BEGIN
    DECLARE @NextMonthStart DATETIME = DATEADD(MONTH, 1, @StartRange)
    INSERT INTO #MonthCalendar (MonthName, MonthStart, MonthEnd)
    VALUES (&#39;Previous Months&#39;, @StartRange, @NextMonthStart)

    SET @StartRange = @NextMonthStart
END

The loop continues until the @StartRange value exceeds the @EndRange value. In each iteration, it will calculate the next month's start date by adding 1 month to the current @StartRange value.

Then, it inserts a row into the #MonthCalendar table with the appropriate values for MonthName, MonthStart, and MonthEnd. As a final step, it will update the @StartRange value to the calculated @NextMonthStart value to proceed to the next iteration.

huangapple
  • 本文由 发表于 2023年6月1日 17:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定