天天看點

GROUPING的用法

<a></a>

grouping::=

<code>GROUPING</code> distinguishes superaggregate rows from regular grouped rows. <code>GROUP</code> <code>BY</code> extensions such as <code>ROLLUP</code> and <code>CUBE</code> produce superaggregate rows where the set of all values is represented by null. Using the <code>GROUPING</code> function, you can distinguish a null representing the set of all values in a superaggregate row from a null in a regular row.

The <code>expr</code> in the <code>GROUPING</code> function must match one of the expressions in the <code>GROUP</code> <code>BY</code> clause. The function returns a value of 1 if the value of <code>expr</code> in the row is a null representing the set of all values. Otherwise, it returns zero. The datatype of the value returned by the <code>GROUPING</code> function is Oracle <code>NUMBER</code>.

In the following example, which uses the sample tables <code>hr.departments</code> and <code>hr.employees</code>, if the <code>GROUPING</code> function returns 1 (indicating a superaggregate row rather than a regular row from the table), then the string "All Jobs" appears in the "JOB" column instead of the null that would otherwise appear: