2017-08-16 8 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で実行できるDatesテーブルが必要です。それから、あなたの日付を列挙するためにそれに完全に参加してください...そして部門をしてください。あなたのデータの日付は常に日曜日であると述べたので、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 

RETURNS

+------+------------+--------------+ 
| 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

これは素晴らしいです。ステートメントの真ん中付近で 'TheDateによるselect * from #tempDates order'をコメントアウトして結果を返すようにコードを取得することができました。私はまだ私の一般的な例から私の特定のデータセットに適合させることに問題がありますが、それは私のものです。ありがとうございました! –

+0

嬉しいことに@MichaelWebb – scsimon

+1

そして今、私はそれを私のデータを揺らしてくれました、ありがとう@scsimon! –

関連する問題