Perhaps the following will give you another idea:
create table dbo.TestSort(
tsid int identity(1,1),
datecol datetime
);
go
/*
Template: Dynamic Tally CTE - 2008
Author: Lynn A. Pettis
Date: 2013-03-12
Site: ISS, Inc -- Colorado Springs, CO
This template is the start of a dynamic Tally table for SQL Server 2008 and later
*/
with
e1(n) as (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)), -- 10 rows
e2(n) as (select 1 from e1 a cross join e1 b), -- 100 rows
eTally(n) as (select row_number() over (order by (select null)) from e2 a cross join e2 b) -- 10,000 rows
insert into dbo.TestSort(datecol)
select
dateadd(day, n-1, getdate())
from
eTally;
go
select
right(convert(varchar, datecol, 106), 8) as startmonth,
count(*)
from
dbo.TestSort
group by
right(convert(varchar, datecol, 106), 8), year(datecol), month(datecol)
order by
year(datecol), month(datecol);
go
drop table dbo.TestSort;
go