这是创建月/年组合并将其用作查询基础的循环:
declare @startDate as datetimeset @startDate = '1/1/13'declare @currentDate as datetimeset @currentDate = '6/6/13'select month(@currentDate) as monthOfDate ,year(@currentDate) as yearOfDateinto #allDateswhere 1=0while (@startDate <= @currentDate)begin insert into #allDates values (month(@startDate),year(@startDate)) set @startDate = dateadd(m,1,@startDate)endselect _monthYear.yearofDate ,_monthYear.monthOfDate , COUNT(p.pId) as totalfrom #allDates _monthYearleft join profile p with(nolock) on month(p.createStamp) = _monthYear.monthOfDate and year(p.createStamp) = _monthYear.yearOfDategroup by _monthYear.yearofDate ,_monthYear.montOfDatedrop table #allDates



