138 High-Function Business Intelligence in e-business
3.3.4 GROUPING, GROUP BY, ROLLUP and CUBE examples
We provide examples using GROUPING, GROUP BY, ROLLUP and CUBE.
GROUPING, GROUP BY and CUBE example
Grouping is used in conjunction with the super-group functions, GROUP BY
CUBE or ROLLUP. The purpose of the GROUPING function is to identify
summary rows in the CUBE and ROLLUP query results. The GROUPING
function returns a one or a zero to indicate whether or not a row returned by the
GROUP BY function is a sub-total row generated by the GROUP BY function.
A one means the row was the result of a sub-total, and a zero means the row was
not the result of a sub-total.
The input to the GROUPING function can be any type, but must be an item of the
associated GROUP BY clause. Consider Example 3-22.
Example 3-22 GROUPING, GROUP BY & CUBE example
SELECT SALES_DATE,
SALES_PERSON,
SUM(SALES) AS UNITS_SOLD,
GROUPING(SALES_DATE) AS DATE_GROUP,
GROUPING(SALES_PERSON) AS SALES_GROUP
FROM SALES
GROUP BY CUBE (SALES_DATE, SALES_PERSON)
ORDER BY SALES_DATE, SALES_PERSON