I need to calculate subtotals in a separate line, but I cannot use ROLLUP or CUBE (because not allowed). I use SQL server 2008.
There is an other way to get follow results? thanks in advance!
SELECT deptno, empno, SUM (sal)
2 FROM emp_test
3 GROUP BY ROLLUP (deptno, empno);
DEPTNO EMPNO SUM(SAL)
10 7782 20000
10 7839 10000
10 30000
20 7566 5950
20 7788 6000
20 7902 6000
20 17950
30 7698 20000
30 7699 20000
30 40000
87950
You might want to try is using the
GROUPING()function on the column. This function returns 1 if it is part of theGROUPING SETSsubtotals, and 0 if it is a regular column. http://technet.microsoft.com/en-us/library/ms178544(SQL.90).aspxI included the sample data I tested with. Remove the first
WITH emp_test AS ()when you use the select statement.My test data:
Actual query below: