2017-08-04 61 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 | 
+------------+---------------+-------+