How to make a time dependent distribution in SQL? -
i have sql table in keep project information coming primavera. suppose have columns start date,end date,duration, , total qty shown below . how can distribute total qty on months using these information. kind of additional columns, sql queries need in order correct monthly distribution?
thanks in advance.
columns in order:
itemname,quantity,startdate,duration,enddate item1 -- 108 -- 2013-03-25 -- 720 -- 2013-07-26 item2 -- 640 -- 2013-03-25 -- 720 -- 2013-07-26 . .
i think key break records apart month. here example of how it:
with months ( select 1 mon union select 2 union select 3 union select 4 mon union select 5 union select 6 union select 7 mon union select 8 union select 9 union select 10 mon union select 11 union select 12 ) select item, m.mon, quantity / nummonths (select t.*, (month(enddate) - month(startdate) + 1) nummonths t ) t join months m on month(t.startdate) <= m.mon , months(t.enddate) >= m.mon;
this works because months within same year -- in example. quite vague on how split should calculated. so, assumed every month start end gets equal amount.
Comments
Post a Comment