我认为关键是按月打破记录。以下是如何执行此操作的示例:
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;
这是有效的,因为所有月份都在同一年内 - 如您的示例所示。您对如何计算拆分非常模糊。所以,我假设每个月从开始到结束都得到相同的数量。