title: Adding Grand Totals in Pivot Table
body: <p>I have created a pivot table with the following code:</p>
  
  <pre><code>DECLARE @SQL AS VARCHAR(MAX)
  DECLARE @Columns AS VARCHAR (MAX)
  
  SELECT @Columns =
  COALESCE(@Columns + ', ','')+ QUOTENAME(PortfolioID)
  FROM
  (
  SELECT PortfolioID FROM InfoPortal.DBO.fn_Generic_PortfolioGroupMembers (@PortfolioGroup)
  ) AS B
  ORDER BY B.PortfolioID
  
  SET @SQL = '
  WITH PivotData AS
  (
      SELECT 
          PortfolioID, Percentage, DurationBand, DurationSort 
      FROM #Worktable
  )
  
  SELECT 
      DurationBand, 
      ' + @Columns + '
  FROM PivotData
  PIVOT
  (
      SUM(Percentage)
      FOR PortfolioID
      IN (' + @Columns + ')
  ) AS PivotResult
  ORDER BY DurationSort'
  
  EXEC (@SQL)
  </code></pre>
  
  <p>However what i would like to do is add grand totals for each portfolioID, and i'm unsure of how to achieve this.  Any help?</p>