declare @firstYear int = 1966 ; with digits as ( select n=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 ) ,years as ( select n=a.n + b.n*10 + c.n*100 + @firstYear from digits a, digits b, digits c where c.n < 4 ) ,yearsExt as ( select n, s=convert(varchar(4),n) from years ) ,months as ( select n=1 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 11 union select 12 ) ,monthsExt as ( select n ,s = convert(varchar(2), n) from months ) ,dates as ( select yr = y.n ,dt = CONVERT(date, y.s + '-' + m.s + '-13') from yearsExt y ,monthsExt m ) ,datesExt as ( select * ,dow = datepart(weekday, dt) ,isFriday = case when datepart(weekday, dt) = 6 then 1 else 0 end from dates ) ,summary as ( select yr ,FridayCount = sum(isFriday) from datesExt group by yr ) select FridayCount ,qty = count(1) from summary group by FridayCount order by FridayCount