GROUP BY CUBE
GROUP BY CUBE is a SQL statement in the Aggregate Functions category. Generates subtotals for ALL combinations of grouping columns. More comprehensive than ROLLUP. The syntax is SELECT col1, col2, SUM(value) FROM table GROUP BY CUBE(col1, col2);. It returns multi-dimensional summary. A typical example: SELECT department, job_title, SUM(salary) FROM employees GROUP BY CUBE(department, job_title); -- Returns: -- IT, Developer, 500K (detail) -- IT, Manager, 300K (detail) -- HR, Recruiter, 200K (detail) -- IT, NULL, 800K (subtotal: department IT) -- HR, NULL, 400K (subtotal: department HR) -- NULL, Developer, 600K (subtotal: job=Developer across all depts) -- NULL, Manager, 350K (subtotal: job=Manager across all depts) -- NULL, NULL, 1.5M (grand total) A close relative is COUNT(), which returns the number of rows (or non-NULL values) in a group. A close relative is SUM(), which returns the sum of all non-NULL values in a group. Works with numeric columns. A close relative is AVG(), which returns the average (mean) of all non-NULL values in a group.