Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL中进行时间相关的分发?_Sql - Fatal编程技术网

如何在SQL中进行时间相关的分发?

如何在SQL中进行时间相关的分发?,sql,Sql,我有一个SQL表,其中保存来自primavera的项目信息。 假设我有如下所示的开始日期、结束日期、持续时间和总数量列。 如何使用这些信息在几个月内分配总数量。为了获得正确的每月分布,我需要什么样的附加列、sql查询 提前谢谢 按顺序排列的列: itemname,quantity,startdate,duration,enddate item1 -- 108 -- 2013-03-25 -- 720 -- 2013-07-26 item2 -- 640 -- 2013-03-25 -- 72

我有一个SQL表,其中保存来自primavera的项目信息。 假设我有如下所示的开始日期、结束日期、持续时间和总数量列。 如何使用这些信息在几个月内分配总数量。为了获得正确的每月分布,我需要什么样的附加列、sql查询

提前谢谢

按顺序排列的列:

itemname,quantity,startdate,duration,enddate

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

我认为关键是按月打破记录。下面是一个如何执行此操作的示例:

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;

这是因为所有月份都在同一年内——如您的示例所示。你对分割的计算方法相当含糊。因此,我假设从开始到结束的每个月都有相同的金额。

如果开始日期和结束日期不在同一个月,如何从开始日期和结束日期计算月份?您使用的是哪种DBMS?博士后?Oracle?我正在使用MSSQL 2008。例如,我可以通过除以数量/持续时间来查找每日数量。但是我不知道如何每月分发这些。谢谢你,不知何故,我通过一个交叉连接的日历表来管理这些分发。