あなたは、動的ピボットを探している場合は、以下の
は、いくつかのサンプルデータ
--Drop Table #YourTable
Create Table #YourTable (CustomerID int,StartDate date,EndDate date,SalesPersonID int,ServicePalnID int,DivisionID int)
Insert Into #YourTable values
(1,'2017-01-01','2017-02-06',1,5,1),
(2,'2017-01-01',null ,1,5,1),
(3,'2017-02-04',null ,1,5,1),
(4,'2017-02-05','2017-04-05',1,5,2),
(5,'2017-06-06',null ,2,6,2),
(6,'2017-03-26','2017-04-03',2,6,2),
(7,'2017-04-01',null ,2,6,3),
(8,'2017-04-04',null ,3,6,3)
を作成し、動的クエリ
Declare @Date1 date = '2017-01-01'
Declare @Date2 date = GetDate()
Declare @SQL varchar(max) = Stuff((Select ',' + QuoteName(format(D,'MMM yyyy'))
From (
Select Top (DateDiff(MONTH,@Date1,@Date2)+1)
D=DateAdd(MONTH,-1+Row_Number() Over (Order By Number),@Date1)
From master..spt_values
) A
For XML Path('')),1,1,'')
Select @SQL = '
Select [YAxis] as [Division],' + @SQL + '
From (
Select YAxis = concat(''Division '',A.DivisionID)
,XAxis = format(D,''MMM yyyy'')
,Value = 1
From #YourTable A
Join (
Select Top (DateDiff(MONTH,'''+concat('',@Date1)+''','''+concat('',@Date2)+''')+1)
D=DateAdd(MONTH,-1+Row_Number() Over (Order By Number),'''+concat('',@Date1)+''')
From master..spt_values
) B
on D between DateFromParts(Year(StartDate),month(StartDate),1) and EOMonth(IsNull(EndDate,GetDate()))
) A
Pivot (sum(Value) For [XAxis] in (' + @SQL + ')) p'
Exec(@SQL);
戻り
を考えます

まず、表示する月が必要です。これにはvalues節を使用できます。その後、テーブルに参加して、1か月に適用されるレコードを取得します。部門と月ごとに集計/ピボット(多くの場合、ピボットのことはSQLではなくアプリで行われます:SQLはデータを取得するため、アプリケーションはプレゼンテーションを気にします)。 –