Hi @Mikhail Firsov ,
Yes, I rarely use ROLLUP and CUBE. I did a simple Google and found an article describing them in detail. hope this helps:
Group By in SQL Sever with CUBE, ROLLUP and GROUPING SETS Examples
In addition, I did a simple test, and your idea does not seem to hold true:
CREATE TABLE #EmpSalary
(
id INT PRIMARY KEY IDENTITY(1,1),
EmpName varchar (200),
Department varchar(100),
Category char(1),
Salary money
)
INSERT #EmpSalary
SELECT 'Bhavesh Patel','IT','A',$8000
UNION ALL
SELECT 'Alpesh Patel','Sales','A',$7000
UNION ALL
SELECT 'Kalpesh Thakor','IT','B',$5000
UNION ALL
SELECT 'Jay Shah','Sales','B',$4000
UNION ALL
SELECT 'Ram Nayak','IT','C',$3000
UNION ALL
SELECT 'Jay Shaw','Sales','C',$2000
UNION ALL
SELECT 'Jay Shaw','Sales','C',$2000;
SELECT
Department,
Category,
SUM(Salary) as Salary
FROM #EmpSalary
GROUP BY CUBE(Category,Department);
Output:
SELECT
Department,
Category,
SUM(Salary) as Salary
FROM #EmpSalary
GROUP BY GROUPING SETS((Category, Department),(Department,Category),Department,
Category,());
Output:
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.