I am using SQL Server 2005 and VS 2008.I am having a query as shown below
SELECT Month
, SUM(Man) AS Man
, SUM(Sal) AS Sal
, SUM(Man + Sal) AS Total
FROM (
SELECT DATENAME(MONTH, DOB) AS Month
, CASE WHEN TypeOfPost = 'Manager' THEN 1 ELSE 0 END AS Man
, CASE WHEN TypeOfPost = 'Sales' THEN 1 ELSE 0 END AS Sal
FROM tableName
) g
GROUP BY
Month
which generates the following output
Output--
-------------------------------------
Month Man Sal Total
-------- ----- ------ ---------
January 1 1 2
June 1 NULL 1
November 1 1 2
But now I want to calculate the total of Total column.So help me out.
Expected Output--
-------------------------------------
Month Man Sal Total
-------- ----- ------ -------
January 1 1 2
June 1 NULL 1
November 1 1 2
-------------------------------------
Total 5
-------------------------------------
You could use the
WITH ROLLUPoption in theGROUP BY– this will give you an extra row that contains the “rolled up” (summed up) values:The extra line will contain
NULLfor theMonthcolumn, and should sum up all other columns for you