【问题标题】:Convert Weekly Total to Daily Average将每周总计转换为每日平均值
【发布时间】:2018-01-24 21:30:46
【问题描述】:

我有一张这样的预测表:

Item      ForecastDate     ForecastQty
A123      7/30/17          140
A123      8/6/17           70
A123      8/13/17          70
A123      8/20/17          70
A123      8/27/17          70
A123      9/3/17           45

我需要得出所有日子(包括周末)的每日平均值,所以我的输出看起来像:

Item      ForecastDate     DailyFcstQty
A123      7/30/17          20
A123      7/31/17          20
A123      8/1/17           20
A123      8/2/17           20
A123      8/3/17           20
A123      8/4/17           20
A123      8/5/17           20
A123      8/6/17           20
A123      8/7/17           10
A123      8/8/17           10

等等。

如何将此每周数据(如果重要,则始终为星期日)转换为这些天的每日平均值?

【问题讨论】:

    标签: sql sql-server-2008 date


    【解决方案1】:

    您需要一个日期表,这可以通过 CTE 完成。然后,完全加入以列举您的日期......并进行除法。由于您声明数据中的日期始终是星期日,因此 7 的除数是静态的,dateadd(day,6,t.ForecastDate) 也是如此。

    declare @table table (Item varchar(4), ForecastDate date, ForecastQty int)
    insert into @table
    values
    ('A123','7/30/17',140),
    ('A123','8/6/17',70),
    ('A123','8/13/17',70),
    ('A123','8/20/17',70),
    ('A123','8/27/17',70),
    ('A123','9/3/17',45)
    
    declare @minDate date = (select min(ForecastDate) from @table)
    declare @maxDate date = (select max(ForecastDate) from @table)
    ;with GetDates As  
    (  
    select @minDate as TheDate              --startdate
    from @table
    UNION ALL  
    select DATEADD(day,1, TheDate) from GetDates  
    where TheDate <= @maxDate   --maxdate
    )
    
    select distinct * 
    into #tempDates 
    from GetDates 
    option(maxrecursion 0)
    
    select * from #tempDates order by TheDate
    select 
        t.Item
        ,d.TheDate
        ,DailyFcstQty = t.ForecastQty / 7
    from @table t
    full outer join
        #tempDates d on 
        d.TheDate >= t.ForecastDate
        and d.TheDate <= dateadd(day,6,t.ForecastDate)
    order by
        t.ForecastDate
    
    
    drop table #tempDates 
    

    返回

    +------+------------+--------------+
    | Item |  TheDate   | DailyFcstQty |
    +------+------------+--------------+
    | A123 | 2017-07-30 |           20 |
    | A123 | 2017-07-31 |           20 |
    | A123 | 2017-08-01 |           20 |
    | A123 | 2017-08-02 |           20 |
    | A123 | 2017-08-03 |           20 |
    | A123 | 2017-08-04 |           20 |
    | A123 | 2017-08-05 |           20 |
    | A123 | 2017-08-06 |           10 |
    | A123 | 2017-08-07 |           10 |
    | A123 | 2017-08-08 |           10 |
    | A123 | 2017-08-09 |           10 |
    | A123 | 2017-08-10 |           10 |
    | A123 | 2017-08-11 |           10 |
    | A123 | 2017-08-12 |           10 |
    | A123 | 2017-08-13 |           10 |
    | A123 | 2017-08-14 |           10 |
    | A123 | 2017-08-15 |           10 |
    | A123 | 2017-08-16 |           10 |
    | A123 | 2017-08-17 |           10 |
    | A123 | 2017-08-18 |           10 |
    | A123 | 2017-08-19 |           10 |
    | A123 | 2017-08-20 |           10 |
    | A123 | 2017-08-21 |           10 |
    | A123 | 2017-08-22 |           10 |
    | A123 | 2017-08-23 |           10 |
    | A123 | 2017-08-24 |           10 |
    | A123 | 2017-08-25 |           10 |
    | A123 | 2017-08-26 |           10 |
    | A123 | 2017-08-27 |           10 |
    | A123 | 2017-08-28 |           10 |
    | A123 | 2017-08-29 |           10 |
    | A123 | 2017-08-30 |           10 |
    | A123 | 2017-08-31 |           10 |
    | A123 | 2017-09-01 |           10 |
    | A123 | 2017-09-02 |           10 |
    | A123 | 2017-09-03 |            6 |
    | A123 | 2017-09-04 |            6 |
    +------+------------+--------------+
    

    【讨论】:

    • 这很棒。在注释掉语句中间附近的“select * from #tempDates order by TheDate”后,我能够让您的代码返回结果。我仍然无法将它从我的一般示例调整到我的特定数据集,但这取决于我。谢谢!
    • 现在我已经掌握了我的数据,谢谢@scsimon!
    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 2017-02-22
    • 2012-08-07
    • 2015-07-14
    • 1970-01-01
    • 2017-11-10
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多