码迷,mamicode.com
首页 > 数据库 > 详细

SQL 循环30日

时间:2016-03-29 19:16:33      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

 

循环30日的统计

大概格式是

技术分享

with Date as (
select 
    cast(DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)  as datetime) 
    Date union all select Date+1 
from Date 
where Date+1 < CONVERT(CHAR(10),DATEADD(m,1,DATEADD(dd,-DAY(GETDATE())+1,GETDATE())),111)
) 
select day(a.Date) as day,
    b.ID,
from Date a  
left join (
select * from Table) b on 
    year(a.Date) = year(b.DATE) 
and month(a.Date) = month(b.DATE) 
and day(a.Date) = day(b.DATE);

SQL 循环30日

标签:

原文地址:http://www.cnblogs.com/objctccc/p/5334017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!