SQL计算日期之间的月份

时间:2015-11-20 04:13:12

标签: sql-server

考虑以下数据:

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年9月。

我如何计算每月当前的租约数量?输出应如下所示:

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
…         …         …

我知道我以前做过,但是很久以前我记得它很乱。提前谢谢!

2 个答案:

答案 0 :(得分:1)

def alterDatabase(self, columns):
    with self.db.transaction():
        columnField = CharField(null=True)
        for column in columns:              
            migrate(migrator.add_column("firsttable", column, columnField))
        columnField.add_to_class(FirstTable, column)  # Add the field to the model.

答案 1 :(得分:0)

这是一个非常合乎逻辑的问题,最后我创建了sql,它给出了所需的结果..我验证了每个日期和月份的数量,并且一切正常。

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