The GROUPING function determines the nesting of the grouping level, returning 0 for the outermost grouping and 1 for the inner grouping. Can be used to identify the outermost grouping, calculate aggregate function results, and create conditions based on grouping.
GROUPING Function in Oracle
The GROUPING function is used to mark grouping levels in aggregate function calculations. It returns a value indicating whether the row belongs to the most deeply nested group.
Syntax
<code>GROUPING(expr)</code>
Where:
Usage
The GROUPING function can be used to:
Return Value
The GROUPING function returns the following values:
Example
<code>SELECT department_id, job_id, SUM(salary) AS total_salary, GROUPING(job_id) AS group_level FROM employee GROUP BY department_id, job_id;</code>
Result:
department_id | job_id | total_salary | group_level |
---|---|---|---|
20 | 60000 | 0 | |
30 | 40000 | 0 | |
40 | 20000 | 1 | |
50 | 50000 | 0 | |
60 | 30000 | 1 |
The above is the detailed content of How to use grouping in oracle. For more information, please follow other related articles on the PHP Chinese website!