GROUPING()
GROUPING() is a SQL function in the Aggregate Functions category. Identifies which rows are subtotal/super-aggregate rows in GROUP BY ROLLUP/CUBE/GROUPING SETS. The syntax is SELECT col1, col2, GROUPING(col1) AS grp FROM table GROUP BY ROLLUP(col1, col2);. It accepts col1. It returns 1 if super-aggregate, 0 if detail. A typical example: SELECT department, job_title, SUM(salary) AS total, GROUPING(department) AS dept_grp, GROUPING(job_title) AS job_grp FROM employees GROUP BY ROLLUP(department, job_title); -- dept_grp=0,job_grp=0 = detail row -- dept_grp=0,job_grp=1 = subtotal by department -- dept_grp=1,job_grp=1 = 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. More about this category: Aggregation functions — COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING.