0

プリマベーラからのプロジェクト情報を保持する SQL テーブルがあります。以下に示すように、開始日、終了日、期間、および合計数量の列があるとします。これらの情報を使用して、ごとに合計数量を配布するにはどうすればよいですか。正しい月次分布を取得するには、どのような追加の列、SQL クエリが必要ですか?

前もって感謝します。

列の順序:

itemname,quantity,startdate,duration,enddate

item1 -- 108 -- 2013-03-25 -- 720 -- 2013-07-26 
item2 -- 640 -- 2013-03-25 -- 720 -- 2013-07-26
  .
  .
4

1 に答える 1

0

重要なのは、記録を月ごとに分割することだと思います。これを行う方法の例を次に示します。

with months as (
     select 1 as mon union all select 2 union all select 3 union all
     select 4 as mon union all select 5 union all select 6 union all
     select 7 as mon union all select 8 union all select 9 union all
     select 10 as mon union all select 11 union all select 12
    )
select item, m.mon, quantity / nummonths
from (select t.*, (month(enddate) - month(startdate) + 1) as nummonths
      from t
     ) t join
     months m
     on month(t.startDate) <= m.mon and
        months(t.endDate) >= m.mon;

あなたの例のように、すべての月が同じ年にあるため、これは機能します。分割の計算方法についてはかなりあいまいです。ですので、毎月最初から最後まで同じ金額を受け取ると仮定しました。

于 2013-03-25T13:29:59.983 に答える