2017-08-04 73 views
1

问题

在我的SQL服务器2014我存储项目与列的表:从时间跨度选择多行

开始日期 .. | 结束日期 .... | 项目名称 ................. |
2017-02-13 | 2017-04-12 | GenerateRevenue ......... | 20.02
2017-04-02 | 2018-01-01 | BuildRevenueGenerator | 300.044
2017-05-23 | 2018-03-19 | HarvestRevenue ............ | 434.009

我需要一个SELECT给我每个项目的项目每月一行。这个月的日子不需要考虑。

日期 .......... | 项目名称 .................. | Volume
2017-02-01 | GenerateRevenue ......... | 20.02
2017-03-01 | GenerateRevenue ......... | 20.02
2017-04-01 | GenerateRevenue ......... | 20.02
2017-04-01 | BuildRevenueGenerator | 300.044
2017-05-01 | BuildRevenueGenerator | 300.044
2017-06-01 | BuildRevenueGenerator | 300.044
...

额外

理想的选择的逻辑让我既来计算月成交量也每个月和以前的区别。

日期 .......... | 项目名称 .................. | VolumeMonthly
2017-02-01 | GenerateRevenue ......... | 6.6733
2017-03-01 | GenerateRevenue ......... | 6.6733
2017-04-01 | GenerateRevenue ......... | 6.6733
2017-04-01 | BuildRevenueGenerator | 30.0044
2017-05-01 | BuildRevenueGenerator | 30.0044
2017-06-01 | BuildRevenueGenerator | 30.0044
...

也...

我知道我可以映射它放在一个临时万年历表,但往往会变得过于庞大和复杂的非常快。我真的想找一个更好的方法来解决这个问题。

解决方案

戈登解决方案的工作非常精美,它不需要对某种类型的日历中的第二个表或映射。虽然我不得不改变一些东西,比如确保工会的双方都有相同的SELECT。

这里我改编版:

with cte as (
    select startdate as mondate, enddate, projectName, volume 
    from projects 
    union all 
    select dateadd(month, 1, mondate), enddate, projectName, volume 
    from cte 
    where eomonth(dateadd(month, 1, mondate)) <= eomonth(enddate) 
) 
select * from cte; 

量每月可以通过更换容积来实现:

CAST(Cast(volume AS DECIMAL)/Cast(Datediff(month, 
startdate,enddate)+ 1 AS DECIMAL) AS DECIMAL(15, 2)) 
END AS [volumeMonthly] 
+3

我推荐宝宝的步骤。 –

+1

样本数据和期望的结果将有所帮助。你也应该简化问题。 –

+0

谢谢戈登,你是对的。我添加了一些你建议的样本数据。 – LeComte

回答

0

您可以使用一个递归子查询扩展行对每一个项目的基础上,表:

with cte as (
     select stardate as mondate, p.* 
     from projects 
     union all 
     select dateadd(month, 1, mondate), . . . -- whatever columns you want here 
     from cte 
     where eomonth(dateadd(month, 1, mondate)) <= eomonth(enddate) 
    ) 
select * 
from cte; 

我不确定这是否真的回答你的问题。当我第一次读到这个问题时,我觉得这个表每个项目都有一行。

+0

是的你是对的,每个项目有一行 – LeComte

1

另一种选择是一个特设的理货表

-- Some Sample Data 
Declare @YourTable table (StartDate date,EndDate date,ProjectName varchar(50), Volume float) 
Insert Into @YourTable values 
('2017-03-15','2017-07-25','Project X',25) 
,('2017-04-01','2017-06-30','Project Y',50) 

-- Set Your Desired Date Range 
Declare @Date1 date = '2017-01-01' 
Declare @Date2 date = '2017-12-31' 

Select Period = D 
     ,B.* 
     ,MonthlyVolume = sum(Volume) over (Partition By convert(varchar(6),D,112)) 
From (Select Top (DateDiff(MONTH,@Date1,@Date2)+1) D=DateAdd(MONTH,-1+Row_Number() Over (Order By (Select Null)),@Date1) 
     From master..spt_values n1 
    ) A 
Join @YourTable B on convert(varchar(6),D,112) between convert(varchar(6),StartDate,112) and convert(varchar(6),EndDate,112) 
Order by Period,ProjectName 

返回

enter image description here

注:使用LEFT JOIN瑟Ë差距

0

使用一对夫妇的common table expressions,自组织日历表个月lag()为最终delta计算(SQL服务器2012+):

create table projects (id int identity(1,1), StartDate date, EndDate date, ProjectName varchar(32), Volume float); 
insert into projects values ('20170101','20170330','SO Q1',240),('20170214','20170601','EX Q2',120) 

declare @StartDate date = '20170101' 
     , @EndDate date = '20170731'; 
;with Months as (
    select top (datediff(month,@startdate,@enddate)+1) 
     MonthStart = dateadd(month, row_number() over (order by number) -1, @StartDate) 
     , MonthEnd = dateadd(day,-1,dateadd(month, row_number() over (order by number), @StartDate)) 
    from master.dbo.spt_values 
) 
, ProjectMonthlyVolume as (
    select p.* 
    , MonthlyVolume = Volume/(datediff(month,p.StartDate,p.EndDate)+1) 
    , m.MonthStart 
    from Months m 
    left join Projects p 
     on p.EndDate >= m.MonthStart 
    and p.StartDate <= m.MonthEnd 
) 
select 
    MonthStart = convert(char(7),MonthStart,120) 
    , MonthlyVolume = isnull(sum(MonthlyVolume),0) 
    , Delta = isnull(sum(MonthlyVolume),0) - lag(Sum(MonthlyVolume)) over (order by MonthStart) 
from ProjectMonthlyVolume pmv 
group by MonthStart 

rextester演示:http://rextester.com/DZL54787

回报:

+------------+---------------+-------+ 
| MonthStart | MonthlyVolume | Delta | 
+------------+---------------+-------+ 
| 2017-01 |   80 | NULL | 
| 2017-02 |   104 | 24 | 
| 2017-03 |   104 | 0  | 
| 2017-04 |   24 | -80 | 
| 2017-05 |   24 | 0  | 
| 2017-06 |   24 | 0  | 
| 2017-07 |    0 | -24 | 
+------------+---------------+-------+