【问题标题】:Consecutive units for next 4 weeks based record date against each product根据每个产品的记录日期,接下来 4 周的连续单位
【发布时间】:2019-09-24 09:04:08
【问题描述】:

/***********输入数据************/

Product# Date        Units
P1  9/1/2019    10
P1  9/04/2019   20
P2  9/27/2019   30

我需要在接下来的 4 周内分配上述产品单元,如下所示,

  1. 对于 Product 的当前周,其单位应为 0。
  2. 和接下来的 4 周基于上述示例数据副产品中给出的日期,它应该是相同的单位。
    • 例如,P1 产品-第 1 周开始日期为 09/02/2019(周开始日期为星期一)单位将为 0,接下来的 4 周如 09/09,09/16,09/23,09/30产品 P1 的单位数将为 10。

/***********输出数据************/

Product#  Date        Weeks  start  Units
P1  9/1/2019    9/2/2019    0
P1  9/1/2019    9/9/2019    10
P1  9/1/2019    9/16/2019   10
P1  9/1/2019    9/23/2019   10
P1  9/1/2019    09/30/019   10
P1  9/4/2019    9/2/2019    0
P1  9/4/2019    9/9/2019    20
P1  9/4/2019    9/16/2019   20
P1  9/4/2019    9/23/2019   20
P1  9/4/2019    09/30/019   20
P2  9/27/2019   9/23/2019   0
P2  9/27/2019   09/30/019   30
P2  9/27/2019   10/7/2019   30
P2  9/27/2019   10/14/2019  30
P2  9/27/2019   10/21/2019  30

/************************输入************************ ***/

Product Id  Date    Units
P1  9/1/2019    10
P1  9/15/2019   20
P2  9/23/2019   30

/************************ 输出 ********************* ***/

Product#    Date    Weeks  start    Units
P1  9/1/2019    9/2/2019    0
P1  9/1/2019    9/9/2019    10
P1  9/1/2019    9/16/2019   10
P1  9/1/2019    9/23/2019   10
P1  9/1/2019    09/30/019   10
P1  9/4/2019    9/2/2019    0
P1  9/4/2019    9/9/2019    20
P1  9/4/2019    9/16/2019   20
P1  9/4/2019    9/23/2019   20
P1  9/4/2019    09/30/019   20
P2  9/27/2019   9/23/2019   0
P2  9/27/2019   09/30/019   30
P2  9/27/2019   10/7/2019   30
P2  9/27/2019   10/14/2019  30
P2  9/27/2019   10/21/2019  30

【问题讨论】:

    标签: sql


    【解决方案1】:

    您似乎希望将星期一作为一周的第一天。 datefirst 这样做。 由于您已多次列出 P1,因此数据未标准化且不易排序。我已经获取了数据并添加了一个 sortorder 行进行排序。它基本上是一个排序后的唯一 ID。

    之后,计算一周的第一天,然后将其与接下来 4 周的每一天联合起来。

    set datefirst 1
    
    declare @Input table (Product varchar(2), Product_Date date, Units int)
    
    insert into @Input
       values('P1','2019-09-01',10),
             ('P1','2019-09-04',20),
             ('P2','2019-09-27',30)
    
    declare @Input_Normalized table
       (Product varchar(2), Product_Date date, Units int, SortOrder int)
    
    insert into @Input_Normalized
    select *, row_number() over (order by Product, Product_Date, Units) SortOrder
    from @Input
    
    select *
    from (
       select Product, Product_Date,
          DATEADD(DAY, 1 - DATEPART(WEEKDAY, Product_Date), Product_Date) Week_Start,
          0 Units, SortOrder
       from @Input_Normalized
    ) a
    union all
    (
       select Product, Product_Date,
          DATEADD(DAY, 8 - DATEPART(WEEKDAY, Product_Date), Product_Date) Week_Start,
          Units, SortOrder
       from @Input_Normalized
    )
    union all
    (
       select Product, Product_Date,
          DATEADD(DAY, 15 - DATEPART(WEEKDAY, Product_Date), Product_Date) Week_Start,
          Units, SortOrder
       from @Input_Normalized
    )
    union all
    (
       select Product, Product_Date,
          DATEADD(DAY, 22 - DATEPART(WEEKDAY, Product_Date), Product_Date) Week_Start,
          Units, SortOrder
       from @Input_Normalized
    )
    union all
    (
       select Product, Product_Date,
          DATEADD(DAY, 29 - DATEPART(WEEKDAY, Product_Date), Product_Date) Week_Start,
          Units, SortOrder
       from @Input_Normalized
    )
    order by SortOrder, Product, Week_Start
    

    请注意,这不会产生与您相同的输出。这是因为 2019 年 9 月 1 日不在 2019 年 9 月 2 日开始的那一周。这似乎是您期望输出的错误。如果 2019 年 9 月 1 日确实应该在 2019 年 9 月 2 日开始的那一周,那么您必须解释如何/为什么。

    【讨论】:

      猜你喜欢
      • 2022-10-12
      • 1970-01-01
      • 2021-03-24
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      相关资源
      最近更新 更多