• Like this:

    DECLARE @StartDate DATE = '20120101';

    WITH Seeds(Seed)

    AS (SELECT *

    FROM ( VALUES ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1), ( 1),

    ( 1), ( 1), ( 1), ( 1), ( 1), ( 1) ) AS V (C)),

    Numbers(Number)

    AS (SELECT ROW_NUMBER() OVER (ORDER BY S1.Seed) - 1

    FROM Seeds AS S1

    CROSS JOIN Seeds AS S2),

    Calendar(Date)

    AS (SELECT DATEADD(DAY, Number, @StartDate)

    FROM Numbers

    WHERE DATEADD(DAY, Number, @StartDate) < DATEADD(YEAR, 1, @StartDate))

    SELECT DATEPART(MONTH, Date),

    COUNT(*)

    FROM Calendar

    WHERE DATEPART(weekday, Date) = 7

    GROUP BY DATEPART(MONTH, Date)

    ORDER BY DATEPART(MONTH, Date);

    I use the Seed and Numbers CTEs to build a table of numbers from 0 - 400, then use the Calendar CTE to build a table of all dates in a year, from the Numbers CTE. It can work even better if you have a persisted Calendar table (those have a lot of good uses). Assuming you don't have one, this will work.

    This solution depends on features from SQL 2008 and later. Based on the forum the question was posted in, that should be okay. If you're actually using a prior version of SQL Server (2005 or earlier), you'll need to change the Seeds CTE so that it uses Union All statements instead of a Table Value Constructor.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon