Обратите внимание на подзапрос
Declare @YourTable Table ([ID] int,[CalendarYear] int,[CalendarMonth] int) Insert Into @YourTable Values
(1,2019,1)
,(2,2019,1)
,(3,2019,2)
Select *
From (
Select CalendarYear
,CalendarMonth
,NumberOfOrders = count(ID)
From @YourTable
Group By CalendarYear,CalendarMonth
) src
Pivot ( sum(NumberOfOrders) for CalendarMonth in ( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) ) pvt
Order By CalendarYear
Возвращает