【问题标题】:How to split week start date and week end date based on custom period dates in another table如何根据另一个表中的自定义期间日期拆分周开始日期和周结束日期
【发布时间】:2020-02-05 14:34:48
【问题描述】:

我有一个场景,我需要在从 1980 年到 2050 年的日历中将以下时段的开始日期和结束日期拆分为整年,时段开始日期和结束日期存储在表格中。

Expected Result for  
1) period start date : 01-01-2020          period end date:   15-01-2020    Period code : P1        
2) period start date : 05-01-2020          period end date:   19-01-2020    Period code : P2     
3) period start date :  16-01-2020         period end date:   31-01-2020    Period code : P1     
4) period start date :  20-01-2020         period end date:   04-02-2020    Period code : P2     

----每个时期应该只分成3周

week_Start_date  week_end_date  period_start_date period_end_date Period_code

**01-01-2020     05-01-2020     01-01-2020        15-01-2020       P1     
06-01-2020       12-01-2020     01-01-2020        15-01-2020       P1     
13-01-2020       15-01-2020     01-01-2020        15-01-2020       P1**   
05-01-2020       05-01-2020     05-01-2020        19-01-2020       P2     
06-01-2020       12-01-2020     05-01-2020        19-01-2020       P2     
13-01-2020       19-01-2020     05-01-2020        19-01-2020       P2     
**16-01-2020     19-01-2020   16-01-2020      31-01-2020           P1     
20-01-2020       26-01-2020     16-01-2020        31-01-2020       P1     
27-01-2020       31-01-2020     16-01-2020        31-01-2020       P1**   
20-01-2020       26-01-2020     20-01-2020        04-02-2020       P2     
27-01-2020       02-02-2020     20-01-2020        04-02-2020       P2     
03-02-2020       04-02-2020     20-01-2020        04-02-2020       P2     

自定义周期表:

year    period_start_date   period_end_date period_code
2020    01-01-2020              15-01-2020  P1   
2020    05-01-2020              19-01-2020  P2   
2020    16-01-2020              31-01-2020  P1   
2020    20-01-2020              04-02-2020  P2   
2020    01-02-2020              15-02-2020  P1   
2020    05-02-2020              19-02-2020  P2   
2020    16-02-2020              29-02-2020  P1   
2020    20-02-2020              04-03-2020  P2   
2020    01-03-2020              15-03-2020  P1   
2020    05-03-2020              19-03-2020  P2   
2020    16-03-2020              31-03-2020  P1   
2020    20-03-2020              04-04-2020  P2   
2020    01-04-2020              15-04-2020  P1   
2020    05-04-2020              19-04-2020  P2   
2020    16-04-2020              30-04-2020  P1   
2020    20-04-2020              04-05-2020  P2   
2020    01-05-2020              15-05-2020  P1   
2020    05-05-2020              19-05-2020  P2   
2020    16-05-2020              31-05-2020  P1   
2020    20-05-2020              04-06-2020  P2   

我在下面的日历表上试过了。

    SELECT *
    FROM [dbo].[CalenderTable] a
        ,(
            SELECT min(DATE) AS week_start_date
                ,max(DATE) AS week_end_date
                ,ISOweek AS ISSWEEK
                ,year AS yyear
                ,month AS mmonth
            FROM [dbo].[dim_cal]
            GROUP BY ISOweek
                ,month
                ,year
            ) b
    WHERE a.DATE BETWEEN b.week_start_date
            AND b.week_end_date

我最终得到了 4 周的时间,这不适合我的情况。我该如何解决这个问题?

【问题讨论】:

  • 如果日期是 YYYY-MM-DD 格式,我会发现这个问题更容易理解。
  • 感谢您的建议,上述问题中提到的格式为 DD-MM-YYYY
  • 。 .我知道。我只是费了很大力气才能弄清楚你真正要问的是什么,我才懒得去弄清楚。
  • 我每个月都有自定义期间,对于每个期间的开始和结束日期,我必须将给定期间的开始日期和结束日期拆分为周开始日期和结束日期,拆分应该只有 3此处的周数 4) 期间开始日期:20-01-2020 期间结束日期:04-02-2020 ------ 结果是 20-01-2020 26-01-2020, 27-01-2020 02 -02-2020 ,03-02-2020 04-02-2020

标签: sql sql-server


【解决方案1】:

终于,这种和平的代码奏效了,但输出中出现了 3 个重复项......

SET NOCOUNT ON;  

DECLARE @period_start_date date, @period_end_date date, @message varchar(1000),
    @period_code nvarchar(10) ;  

PRINT '-------- Starting --------';  

DECLARE vendor_cursor CURSOR FOR   
SELECT top (1) period_start_date , period_end_date, period_code from
[dbo].[MonthlyCalender] where period_start_date='2020-1-20' ORDER BY month_start_date; 

OPEN vendor_cursor  

--FETCH NEXT FROM vendor_cursor   
---INTO @month_start_date, @month_end_date,@period_code  

 ---   PRINT @month_start_date

    WHILE @@FETCH_STATUS = 0
   BEGIN
      FETCH NEXT FROM vendor_cursor   
INTO @period_start_date, @period_end_date,@period_code  

      PRINT @period_start_date
      PRINT @period_end_date
      PRINT @period_code  



declare @StartDate date


 set @StartDate = @period_start_date

 declare @EndDate date

 set @EndDate = @period_end_date

declare @pd_start_date date

set @pd_start_date = @period_start_date

declare @pd_end_date date

set @pd_end_date = @period_end_date

declare @pd_code nvarchar(10)

set @pd_code=@period_code 

 declare @DateCalc date

 declare @WeekStartDate date

 set @WeekStartDate = DATEADD(ww, DATEDIFF(ww,0,@StartDate), 0)

 set @DateCalc = @StartDate

 WHILE (@WeekStartDate <= @EndDate )

 begin

insert into testrun

   select case when @StartDate > @WeekStartDate then DatePart(ww,@StartDate) else DatePart(ww,@WeekStartDate) end as WeekNum, @StartDate as StartDate, case when DATEADD(dd, 6, @WeekStartDate) > @EndDate then @EndDate else DATEADD(dd, 6, @WeekStartDate) end as EndDate,
   @pd_start_date as pd_start_date, @pd_end_date as pd_end_date,@pd_code as pd_code;

   set @WeekStartDate = DATEADD(dd, 7, @WeekStartDate)

   set @StartDate = @WeekStartDate
 END



END;

PRINT 'Done Populating week start and end dates for given periods';


CLOSE vendor_cursor  
DEALLOCATE vendor_cursor;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-09
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    相关资源
    最近更新 更多