2017-08-16 141 views
1

我預測的表像這樣:轉換每週總要每日平均

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 

等等。

如何將這些每週數據(總是一個星期日,如果有要求的話)轉換爲日平均日均值?

回答

0

您需要一個日期表,可以用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 | 
+------+------------+--------------+ 
+0

這是偉大的。在聲明中間附近註釋掉'select * from #tempDates order by TheDate'之後,我能夠讓您的代碼返回結果。我仍然遇到問題,將其從我的一般示例改編爲我的特定數據集,但這是對我的影響。謝謝! –

+0

很高興工作@MichaelWebb – scsimon

+1

現在我已經擺脫了我的數據,謝謝@scsimon! –