

ORDER BY fact_1_id, fact_2_id, fact_3_id GROUPING Functions GROUPING GROUP BY fact_1_id, CUBE (fact_2_id, fact_3_id) The output from the following partial cube is shown here. It is possible to do a partial cube to reduce the number of subtotals calculated. GROUP BY CUBE (fact_1_id, fact_2_id, fact_3_id) If "n" is the number of columns listed in the CUBE, there will be 2 n subtotal combinations.Īs the number of dimensions increase, so do the combinations of subtotals that need to be calculated, as shown by the output of the following query, shown here. In addition to the subtotals generated by the ROLLUP extension, the CUBE extension will generate subtotals for all combinations of the dimensions specified. ORDER BY fact_1_id, fact_2_id, fact_3_id CUBE GROUP BY fact_1_id, ROLLUP (fact_2_id, fact_3_id) The output from the following partial rollup is shown here. It is possible to do a partial rollup to reduce the number of subtotals calculated. GROUP BY ROLLUP (fact_1_id, fact_2_id, fact_3_id) Obviously, if the raw data contains null values, using this visual identification is not an accurate approach, but we will discuss this issue later. It may be easier to spot when scanning down the output of the following query shown here. Looking at the output in a SQL*Plus or a grid output, you can visually identify the rows containing subtotals as they have null values in the ROLLUP columns. If "n" is the number of columns listed in the ROLLUP, there will be n+1 levels of subtotals. In addition to the regular aggregation results we expect from the GROUP BY clause, the ROLLUP extension produces group subtotals from right to left and a grand total. ORDER BY fact_1_id, fact_2_id, fact_3_id įACT_1_ID FACT_2_ID FACT_3_ID NUM_ROWS SALES_VALUE Including the first three columns in the GROUP BY clause should give us 100 rows (2*5*10). Including the first two columns in the GROUP BY clause should give us 10 rows (2*5), each with its aggregated values. So using the fact_1_id column in the GROUP BY clause should give us 2 rows. In this case, if the rows were loaded randomly we would expect the number of distinct values for the first three columns in the table to be 2, 5 and 10 respectively. The number of rows we expect can be calculated by multiplying the number of distinct values of each column listed in the GROUP BY clause.


This way we get an aggregated value for each distinct combination of values present in the columns listed in the GROUP BY clause. Including the GROUP BY clause limits the window of data processed by the aggregate function. ROUND(DBMS_RANDOM.value(low => 1, high => 100), 2) AS sales_value TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_4_id, TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_3_id, TRUNC(DBMS_RANDOM.value(low => 1, high => 6)) AS fact_2_id, SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id, The examples in this article will be run against the following simple dimension table. This article gives an overview of the functionality available for aggregation in data warehouses, focusing specifically on the information required for the Oracle Database SQL Expert (1Z0-047) exam. Home » Articles » Misc » Here ROLLUP, CUBE, GROUPING Functions and GROUPING SETS
