2012-12-06 79 views

回答

0

虛擬纔是你真正的表...

WITH DummyDates (Date) AS (
    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, '20121231') - DATEDIFF(DAY, '20060101', '20121231'), 0) 
     UNION ALL SELECT DATEADD(DAY, 1, Date) 
      FROM DummyDates 
      WHERE DATEADD(DAY, 1, Date) <= '20121231') 
SELECT DatePart(yy,DummyDates.Date) as [Year],DATEPART(MM,DummyDates.Date) as [Month],SUM(Case when dummy.date is null then 0 else 1 end) as cnt 
FROM DummyDates 
LEFT Join dummy on dummy.Date=DummyDates.Date 
Group by DatePart(yy,DummyDates.Date),DATEPART(MM,DummyDates.Date) 
Order by DatePart(yy,DummyDates.Date),DATEPART(MM,DummyDates.Date) 
OPTION (MAXRECURSION 0)