I'm using SQL SERVER 2012 and i am using the below query
SELECT Status AS [Status]
,[Feb-2016] AS [Current(Feb)]
,[Jan-2016]
,[Dec-2015]
,[Nov-2015]
,[Oct-2015]
,[Sep-2015]
,[Aug-2015]
,[Jul-2015]
,[Jun-2015]
,[May-2015]
,[Apr-2015]
,[Mar-2015]
,[Feb-2015]
INTO #TempTable
FROM (
SELECT Status
,[Count]
,[Month]
FROM CTE2--I'm using this Common Table Expression in the SP
) AS [Mnth]
PIVOT(SUM([Count]) FOR [Month] IN (
[Feb-2016]
,[Jan-2016]
,[Dec-2015]
,[Nov-2015]
,[Oct-2015]
,[Sep-2015]
,[Aug-2015]
,[Jul-2015]
,[Jun-2015]
,[May-2015]
,[Apr-2015]
,[Mar-2015]
,[Feb-2015]
)) AS [nNamePivot]
For the above part i can use dynamic Query
If i use dynamic query, how to get output for the below part and I need to use those dynamic columns along with aggregate function to generate reports month wise.
SELECT *
FROM #TempTable
UNION
SELECT 'Total'
,SUM([Current(Feb)])
,SUM([Jan-2016])
,SUM([Dec-2015])
,SUM([Nov-2015])
,SUM([Oct-2015])
,SUM([Sep-2015])
,SUM([Aug-2015])
,SUM([Jul-2015])
,SUM([Jun-2015])
,SUM([May-2015])
,SUM([Apr-2015])
,SUM([Mar-2015])
,SUM([Feb-2015])
FROM #TempTable
Please help me with this.