title: Dynamic pivot in SQL server not grouping correctly
body: <p>I'm trying to build a dynamic pivot that essentially shows subscription amounts by customer by month. The dynamic part of the pivot is working correctly, but the resulting pivot has multiple non-identical lines per customer ID and I'm not sure why. Fair warning, I'm very new to this whole SQL thing so my code may be a complete mess and for that I'm  sorry</p>
  
  <pre><code>DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
  DECLARE @ColumnName AS NVARCHAR(MAX)
  SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
          + QUOTENAME([Month])
  FROM (SELECT DISTINCT TOP 99.99 PERCENT [Month] FROM [Service Contract Data].[dbo].[filtered_data] ORDER BY [Month]) AS [Month]
  SET @DynamicPivotQuery = 
      N'SELECT [Customer ID], ' + @ColumnName + '
      INTO [Service Contract Data].[dbo].[Pivot_Results] FROM [Service Contract Data].[dbo].[filtered_data]
      PIVOT(SUM([Service Contract Data].[dbo].[filtered_data].[Subscription Amount]) 
              FOR [Month] IN (' + @ColumnName + ')) AS PVTTable'
  EXEC sp_executesql @DynamicPivotQuery;
  </code></pre>
  
  <p>@DynamicPivotQuery returns</p>
  
  <pre><code>SELECT [Customer ID], [Jan  1 2013 12:00AM],[Feb  1 2013 12:00AM],[Mar  1 2013 12:00AM],[Apr  1 2013 12:00AM],[May  1 2013 12:00AM],[Jun  1 2013 12:00AM],[Jul  1 2013 12:00AM],[Aug  1 2013 12:00AM],[Sep  1 2013 12:00AM],[Oct  1 2013 12:00AM],[Nov  1 2013 12:00AM],[Dec  1 2013 12:00AM],[Jan  1 2014 12:00AM],[Feb  1 2014 12:00AM],[Mar  1 2014 12:00AM],[Apr  1 2014 12:00AM],[May  1 2014 12:00AM],[Jun  1 2014 12:00AM],[Jul  1 2014 12:00AM],[Aug  1 2014 12:00AM],[Sep  1 2014 12:00AM],[Oct  1 2014 12:00AM],[Nov  1 2014 12:00AM],[Dec  1 2014 12:00AM],[Jan  1 2015 12:00AM],[Feb  1 2015 12:00AM],[Mar  1 2015 12:00AM],[Apr  1 2015 12:00AM],[May  1 2015 12:00AM],[Jun  1 2015 12:00AM],[Jul  1 2015 12:00AM],[Aug  1 2015 12:00AM],[Sep  1 2015 12:00AM],[Oct  1 2015 12:00AM],[Nov  1 2015 12:00AM],[Dec  1 2015 12:00AM],[Jan  1 2016 12:00AM],[Feb  1 2016 12:00AM]
      INTO [Service Contract Data].[dbo].[Pivot_Results] FROM [Service Contract Data].[dbo].[filtered_data]
      PIVOT(SUM([Service Contract Data].[dbo].[filtered_data].[Subscription Amount]) 
              FOR [Month] IN ([Jan  1 2013 12:00AM],[Feb  1 2013 12:00AM],[Mar  1 2013 12:00AM],[Apr  1 2013 12:00AM],[May  1 2013 12:00AM],[Jun  1 2013 12:00AM],[Jul  1 2013 12:00AM],[Aug  1 2013 12:00AM],[Sep  1 2013 12:00AM],[Oct  1 2013 12:00AM],[Nov  1 2013 12:00AM],[Dec  1 2013 12:00AM],[Jan  1 2014 12:00AM],[Feb  1 2014 12:00AM],[Mar  1 2014 12:00AM],[Apr  1 2014 12:00AM],[May  1 2014 12:00AM],[Jun  1 2014 12:00AM],[Jul  1 2014 12:00AM],[Aug  1 2014 12:00AM],[Sep  1 2014 12:00AM],[Oct  1 2014 12:00AM],[Nov  1 2014 12:00AM],[Dec  1 2014 12:00AM],[Jan  1 2015 12:00AM],[Feb  1 2015 12:00AM],[Mar  1 2015 12:00AM],[Apr  1 2015 12:00AM],[May  1 2015 12:00AM],[Jun  1 2015 12:00AM],[Jul  1 2015 12:00AM],[Aug  1 2015 12:00AM],[Sep  1 2015 12:00AM],[Oct  1 2015 12:00AM],[Nov  1 2015 12:00AM],[Dec  1 2015 12:00AM],[Jan  1 2016 12:00AM],[Feb  1 2016 12:00AM])) AS PVTTable
  </code></pre>