日期:2014-05-18  浏览次数:20307 次

不好意思 又问类似问题-根据所选的年、月按天取记录
刚问了一个   根据所选的年按月取记录
http://community.csdn.net/Expert/topic/5488/5488174.xml?temp=.8080255

高手的解答是逐个添加上12个月

可是现在要是根据所选的年、月按天取当月的记录
由于月份的天数不定   该怎么写sql呢?

这个是按月取的
select    
        a.[month],isnull(count(b.LogAddtime),0)   as   [count]
from  
        (
select   1   as   [month]  
union   select   2  
union   select   3
union   select   4
union   select   5
union   select   6
union   select   7
union   select   8
union   select   9
union   select   10
union   select   10
union   select   12)   as   a  
LEFT   OUTER   JOIN
          ManViewLogs
            as   b
on
        a.[month]=datepart(mm,b.LogAddtime)   and   year(b.LogAddtime)=2007
 
group   by   a.[month]


请问按天的话   当月的天怎么全部取出来???

------解决方案--------------------
declare @a table(天 datetime)
declare @i int
set @i=1
while @i <=day(dateadd(d,-1,convert(varchar(8),dateadd(mm,1,getdate()),120)+ '01 '))
begin
insert @a values(dateadd(d,0,convert(varchar(8),dateadd(mm,1,getdate()),120)+cast(@i as varchar(2))))
set @i=@i+1
end
select * from @a


非动态,你继续用@a就可以了
------解决方案--------------------
up