2015-11-20 43 views
1
日期間,其中

考慮下面的數據:SQL數按月

ID Reference Manager LeaseFirstStart   LeaseStop 
1 KLEIN  John 2008-04-02 00:00:00.000 2010-04-01 00:00:00.000 
2 HAWKER  John 2008-12-18 00:00:00.000 2010-09-17 00:00:00.000 
3 SLEEP  Bob  2008-01-23 00:00:00.000 2009-01-22 00:00:00.000 
4 CODD  Bob  2009-08-03 00:00:00.000 2010-08-02 00:00:00.000 
5 ALLEN  Bob  2008-01-30 00:00:00.000 2009-07-31 00:00:00.000 

最早的月份是2008年1月和最近一個月是2010年九月

我怎麼能指望那是租賃的數量每月電流?輸出應該是這樣的:

Month  Number of Leases 
2008-01 2  
2008-02 2  
2008-03 2  
2008-04 3  
2008-05 3  
2008-06 3  
2008-07 3  
2008-08 4  
…   …  

最後,我想用這個問題的答案由用戶創建以下在Excel中使用的數據集,使他們能夠看到在數據週期誰有多少租賃。

Month  Manager Number of Leases 
2008-01 Bob  2 
2008-01 John  0 
2008-02 Bob  2 
2008-02 John  0 
2008-03 Bob  2 
2008-03 John  0 
2008-04 Bob  2 
2008-04 John  1 
2008-05 Bob  2 
2008-05 John  1 
2008-06 Bob  2 
2008-06 John  1 
2008-07 Bob  2 
2008-07 John  1 
2008-08 Bob  3 
2008-08 John  1 
…   …   … 

我知道我以前做過,但是很久以前,我記得它很混亂。提前致謝!

+0

GROUP BY和SUM也許? –

+0

是否需要與其他列詳細信息的年份組合列表? – Ajay2707

回答

0

這是非常合乎邏輯的問題,最後我創造這給預期結果的SQL ..我覈實每一個日期和月份數及其一切ok。

Declare @t table (ID int, Reference varchar(50), Manager varchar(50),LeaseFirstStart datetime,LeaseStop datetime) 
insert into @t 
values 
(1,'KLEIN','John','2008-04-02 00:00:00.000','2010-04-01 00:00:00.000'), 
(2,'HAWKER','John','2008-12-18 00:00:00.000','2010-09-17 00:00:00.000'), 
(3,'SLEEP','Bob','2008-01-23 00:00:00.000','2009-01-22 00:00:00.000'), 
(4,'CODD','Bob','2009-08-03 00:00:00.000','2010-08-02 00:00:00.000'), 
(5,'ALLEN','Bob','2008-02-28 00:00:00.000','2009-07-31 00:00:00.000') 


declare @lowerdate datetime , @currentdt datetime 
select @lowerdate = min(leasefirststart), @currentdt= max(leasestop) from @t 

;with cte as 
( 
    select firstday,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, FirstDay) + 1, 0)) Lastday, mng from 
    (select dateadd(m,datediff(m,0,@lowerdate)+v.number,0) as FirstDay  
     From master..spt_values v 
     Where v.type='P' and v.number between 0 and datediff(m, @lowerdate, @currentdt) 
    ) as a 
    , (select distinct manager mng from @t) as b 
) 

select (convert(varchar,datepart (yyyy,FirstDay)) + '-' + convert(varchar, MONTH(FirstDay))) MonthAndYear ,mng as mng , count(manager) cnt 
from cte 
left join @t on 
( 
    firstday between LeaseFirstStart and LeaseStop 
    or 
    Lastday between LeaseFirstStart and LeaseStop 
) and cte.mng = Manager 
group by firstday, mng 
order by FirstDay 
+0

這很美。謝謝,我希望其他人能從中受益。 – Warren

1
select sum (no) as no,datet from (SELECT COUNT (*) as no ,(convert(varchar,datepart (yyyy,[ Start])) + '-' + convert(varchar, MONTH([ Start]))) as datet 
FROM <tbl> 
GROUP BY (convert(varchar,datepart (yyyy,[ Start])) + '-' + convert(varchar, MONTH([ Start]))) 
union SELECT COUNT (*) as no ,(convert(varchar,datepart (yyyy,[ End])) + '-' + convert(varchar, MONTH([ End]))) as datet 
FROM <tbl> 
GROUP BY (convert(varchar,datepart (yyyy,[ End])) + '-' + convert(varchar, MONTH([ End])))) t 
+0

不要給同一個問題2個答案,使用編輯選項而不是新答案。 – Ajay2707