Oracle-Base - Rollup, Cube, Grouping Functions and Grouping Sets
Oracle-Base - Rollup, Cube, Grouping Functions and Grouping Sets
Home(/)Articles(/articles)Misc(/articles/misc)Here
ROLLUP,CUBE,GROUPINGFunctionsand
GROUPINGSETS
Thisarticlegivesanoverviewofthefunctionalityavailableforaggregationindata
warehouses,focusingspecificallyontheinformationrequiredfortheOracle
DatabaseSQLExpert(1Z0047)(http://education.oracle.com/pls/web_prodplq
dad/db_pages.getpage?
page_id=41&p_org_id=1001&lang=US&p_exam_id=1Z0_047)exam. Machine Learning eBook
Basics To Advanced Techniques.
Setup Get Access To Examples, Videos
GROUPBY & More. Then Try MATLAB
ROLLUP
CUBE
GROUPINGFunctions
GROUPINGFunction
GROUPING_IDFunction
GROUP_IDFunction
GROUPINGSETS
CompositeColumns
ConcatenatedGroupings
Formoreinformationsee:
GROUPBY,ROLLUPandCUBEinOracle(https://www.youtube.com/watch?v=CCm4IYNtfw)
Setup
Theexamplesinthisarticlewillberunagainstthefollowingsimpledimensiontable.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 1/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
DROPTABLEdimension_tab;
CREATETABLEdimension_tab(
fact_1_idNUMBERNOTNULL,
fact_2_idNUMBERNOTNULL,
fact_3_idNUMBERNOTNULL,
fact_4_idNUMBERNOTNULL,
sales_valueNUMBER(10,2)NOTNULL
);
INSERTINTOdimension_tab
SELECTTRUNC(DBMS_RANDOM.value(low=>1,high=>3))ASfact_1_id,
TRUNC(DBMS_RANDOM.value(low=>1,high=>6))ASfact_2_id,
TRUNC(DBMS_RANDOM.value(low=>1,high=>11))ASfact_3_id,
TRUNC(DBMS_RANDOM.value(low=>1,high=>11))ASfact_4_id,
ROUND(DBMS_RANDOM.value(low=>1,high=>100),2)ASsales_value
FROMdual
CONNECTBYlevel<=1000;
COMMIT;
TokeepthequeriesandtheiroutputsimpleIamgoingtoignorethefacttablesandalsolimitthenumberofdistinctvaluesinthe
columnsofthedimensiontable.
GROUPBY
Let'sstartberemindingourselveshowthe GROUPBY clauseworks.Anaggregatefunctiontakesmultiplerowsofdatareturnedbya
queryandaggregatesthemintoasingleresultrow.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 2/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTSUM(sales_value)ASsales_value
FROMdimension_tab;
SALES_VALUE
50528.39
1rowselected.
SQL>
SELECTfact_1_id,
COUNT(*)ASnum_rows,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYfact_1_id
ORDERBYfact_1_id;
FACT_1_IDNUM_ROWSSALES_VALUE
147824291.35
252226237.04
2rowsselected.
SQL>
SELECTfact_1_id,
fact_2_id,
COUNT(*)ASnum_rows,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYfact_1_id,fact_2_id
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDNUM_ROWSSALES_VALUE
11834363.55
12964794.76
13934718.25
141055387.45
151015027.34
211095652.84
22964583.02
231105555.77
241135936.67
25944508.74
10rowsselected.
SQL>
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 4/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
fact_3_id,
COUNT(*)ASnum_rows,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYfact_1_id,fact_2_id,fact_3_id
ORDERBYfact_1_id,fact_2_id,fact_3_id;
FACT_1_IDFACT_2_IDFACT_3_IDNUM_ROWSSALES_VALUE
11110381.61
1126235.29
1137270.7
11413634.05
11510602.36
1167538.41
1175245.87
1188435.54
1198506.59
11109513.13
...
25114714.84
25213686.56
25313579.5
25410336.87
2555215.17
2564268.72
25714667.22
2587451.29
2598365.24
25106223.33
100rowsselected.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 5/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SQL>
ROLLUP
Inadditiontotheregularaggregationresultsweexpectfromthe GROUPBY clause,the ROLLUP extensionproducesgroupsubtotals
fromrighttoleftandagrandtotal.If"n"isthenumberofcolumnslistedinthe ROLLUP ,therewillben+1levelsofsubtotals.
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYROLLUP(fact_1_id,fact_2_id)
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDSALES_VALUE
114363.55
124794.76
134718.25
145387.45
155027.34
124291.35
215652.84
224583.02
235555.77
245936.67
254508.74
226237.04
50528.39
13rowsselected.
SQL>
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 6/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
LookingattheoutputinaSQL*Plusoragridoutput,youcanvisuallyidentifytherowscontainingsubtotalsastheyhavenullvalues
inthe ROLLUP columns.Itmaybeeasiertospotwhenscanningdowntheoutputofthefollowingqueryshownhere(rollup.txt).
Obviously,iftherawdatacontainsnullvalues,usingthisvisualidentificationisnotanaccurateapproach,butwewilldiscussthis
issuelater.
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYROLLUP(fact_1_id,fact_2_id,fact_3_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id;
Itispossibletodoapartialrolluptoreducethenumberofsubtotalscalculated.Theoutputfromthefollowingpartialrollupisshown
here(rolluppartial.txt).
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYfact_1_id,ROLLUP(fact_2_id,fact_3_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id;
CUBE
Inadditiontothesubtotalsgeneratedbythe ROLLUP extension,the CUBE extensionwillgeneratesubtotalsforallcombinationsof
thedimensionsspecified.If"n"isthenumberofcolumnslistedinthe CUBE ,therewillbe2nsubtotalcombinations.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 7/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYCUBE(fact_1_id,fact_2_id)
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDSALES_VALUE
114363.55
124794.76
134718.25
145387.45
155027.34
124291.35
215652.84
224583.02
235555.77
245936.67
254508.74
226237.04
110016.39
29377.78
310274.02
411324.12
59536.08
50528.39
18rowsselected.
SQL>
Asthenumberofdimensionsincrease,sodothecombinationsofsubtotalsthatneedtobecalculated,asshownbytheoutputofthe
followingquery,shownhere(cube.txt).
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 8/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYCUBE(fact_1_id,fact_2_id,fact_3_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id;
Itispossibletodoapartialcubetoreducethenumberofsubtotalscalculated.Theoutputfromthefollowingpartialcubeisshown
here(cubepartial.txt).
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value
FROMdimension_tab
GROUPBYfact_1_id,CUBE(fact_2_id,fact_3_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id;
GROUPINGFunctions
GROUPING
Itcanbequiteeasytovisuallyidentifysubtotalsgeneratedbyrollupsandcubes,buttodoitprogramaticallyyoureallyneed
somethingmoreaccuratethanthepresenceofnullvaluesinthegroupingcolumns.Thisiswherethe GROUPING functioncomesin.It
acceptsasinglecolumnasaparameterandreturns"1"ifthecolumncontainsanullvaluegeneratedaspartofasubtotalbya
ROLLUP or CUBE operationor"0"foranyothervalue,includingstorednullvalues.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 9/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value,
GROUPING(fact_1_id)ASf1g,
GROUPING(fact_2_id)ASf2g
FROMdimension_tab
GROUPBYCUBE(fact_1_id,fact_2_id)
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDSALES_VALUEF1GF2G
114363.5500
124794.7600
134718.2500
145387.4500
155027.3400
124291.3501
215652.8400
224583.0200
235555.7700
245936.6700
254508.7400
226237.0401
110016.3910
29377.7810
310274.0210
411324.1210
59536.0810
50528.3911
18rowsselected.
SQL>
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 10/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
Fromthiswecansee:
Itwouldnowbeeasytowriteaprogramtoaccuratelyprocessthedata.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 11/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value,
GROUPING(fact_1_id)ASf1g,
GROUPING(fact_2_id)ASf2g
FROMdimension_tab
GROUPBYCUBE(fact_1_id,fact_2_id)
HAVINGGROUPING(fact_1_id)=1ORGROUPING(fact_2_id)=1
ORDERBYGROUPING(fact_1_id),GROUPING(fact_2_id);
FACT_1_IDFACT_2_IDSALES_VALUEF1GF2G
124291.3501
226237.0401
411324.1210
310274.0210
29377.7810
110016.3910
59536.0810
50528.3911
8rowsselected.
SQL>
GROUPING_ID
The GROUPING_ID functionprovidesanalternateandmorecompactwaytoidentifysubtotalrows.Passingthedimensioncolumnsas
arguments,itreturnsanumberindicatingthe GROUPBY level.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 12/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id)ASgrouping_id
FROMdimension_tab
GROUPBYCUBE(fact_1_id,fact_2_id)
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDSALES_VALUEGROUPING_ID
114363.550
124794.760
134718.250
145387.450
155027.340
124291.351
215652.840
224583.020
235555.770
245936.670
254508.740
226237.041
110016.392
29377.782
310274.022
411324.122
59536.082
50528.393
18rowsselected.
SQL>
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 13/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
GROUP_ID
It'spossibletowritequeriesthatreturntheduplicatesubtotals,whichcanbealittleconfusing.The GROUP_ID functionassignsthe
value"0"tothefirstset,andallsubsequentsetsgetassignedahighernumber.Thefollowingqueryforcesduplicatestoshowthe
GROUP_ID functioninaction.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 14/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id)ASgrouping_id,
GROUP_ID()ASgroup_id
FROMdimension_tab
GROUPBYGROUPINGSETS(fact_1_id,CUBE(fact_1_id,fact_2_id))
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDSALES_VALUEGROUPING_IDGROUP_ID
114363.5500
124794.7600
134718.2500
145387.4500
155027.3400
124291.3511
124291.3510
215652.8400
224583.0200
235555.7700
245936.6700
254508.7400
226237.0411
226237.0410
110016.3920
29377.7820
310274.0220
411324.1220
59536.0820
50528.3930
20rowsselected.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 15/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SQL>
Ifnecessary,youcouldthenfiltertheresultsusingthegroup.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 16/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id)ASgrouping_id,
GROUP_ID()ASgroup_id
FROMdimension_tab
GROUPBYGROUPINGSETS(fact_1_id,CUBE(fact_1_id,fact_2_id))
HAVINGGROUP_ID()=0
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDSALES_VALUEGROUPING_IDGROUP_ID
114363.5500
124794.7600
134718.2500
145387.4500
155027.3400
124291.3510
215652.8400
224583.0200
235555.7700
245936.6700
254508.7400
226237.0410
110016.3920
29377.7820
310274.0220
411324.1220
59536.0820
50528.3930
18rowsselected.
SQL>
Translate
GROUPINGSETS
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 17/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
GROUPINGSETS
Calculatingallpossiblesubtotalsinacube,especiallythosewithmanydimensions,canbequiteanintensiveprocess.Ifyoudon't
needallthesubtotals,thiscanrepresentaconsiderableamountofwastedeffort.Thefollowingcubewiththreedimensionsgives8
levelsofsubtotals(GROUPING_ID:07),shownhere(groupingsetscube.txt).
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id,fact_3_id)ASgrouping_id
FROMdimension_tab
GROUPBYCUBE(fact_1_id,fact_2_id,fact_3_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id;
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 18/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id,fact_3_id)ASgrouping_id
FROMdimension_tab
GROUPBYGROUPINGSETS((fact_1_id,fact_2_id),(fact_1_id,fact_3_id))
ORDERBYfact_1_id,fact_2_id,fact_3_id;
FACT_1_IDFACT_2_IDFACT_3_IDSALES_VALUEGROUPING_ID
114363.551
124794.761
134718.251
145387.451
155027.341
112737.42
121854.292
132090.962
142605.172
152590.932
162506.92
171839.852
182953.042
192778.752
1102334.062
215652.841
224583.021
235555.771
245936.671
254508.741
213512.692
222847.942
232972.52
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 19/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
242534.062
253115.992
262775.852
272208.192
282358.552
291884.112
2102027.162
30rowsselected.
SQL>
Noticehowwehavegonefromreturning198rowswith8subtotallevelsinthecube,tojust30rowswith2subtotallevels.
CompositeColumns
ROLLUP and CUBE considereachcolumnindependentlywhendecidingwhichsubtotalsmustbecalculated.For ROLLUP thismeans
steppingbackthroughthelisttodeterminethegroupings.
ROLLUP(a,b,c)
(a,b,c)
(a,b)
(a)
()
CUBE createsagroupingforeverypossiblecombinationofcolumns.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 20/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
CUBE(a,b,c)
(a,b,c)
(a,b)
(a,c)
(a)
(b,c)
(b)
(c)
()
Compositecolumnsallowcolumnstobegroupedtogetherwithbracessotheyaretreatedasasingleunitwhendeterminingthe
necessarygroupings.Inthefollowing ROLLUP columns"a"and"b"havebeenturnedintoacompositecolumnbytheadditional
braces.Asaresultthegroupof"a"isnotlongercalculatedasthecolumn"a"isonlypresentaspartofthecompositecolumninthe
statement.
ROLLUP((a,b),c)
(a,b,c)
(a,b)
()
Notconsidered:
(a)
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 21/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
CUBE((a,b),c)
(a,b,c)
(a,b)
(c)
()
Notconsidered:
(a,c)
(a)
(b,c)
(b)
Theimpactofthisisshownclearlyinthefollowtwostatements,whoseoutputisshownhere(compositecolumn1.txt)andhere
(compositecolumn2.txt).Theregularcubereturns198rowsand8groups(07),whilethecubewiththecompositecolumnreturns
only121rowswith4groups(0,1,6,7)
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 22/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
RegularCube.
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id,fact_3_id)ASgrouping_id
FROMdimension_tab
GROUPBYCUBE(fact_1_id,fact_2_id,fact_3_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id;
Cubewithcompositecolumn.
SELECTfact_1_id,
fact_2_id,
fact_3_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id,fact_3_id)ASgrouping_id
FROMdimension_tab
GROUPBYCUBE((fact_1_id,fact_2_id),fact_3_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id;
ConcatenatedGroupings
Concatenatedgroupingsaredefinedbyputtingtogethermultiple GROUPINGSETS , CUBE sor ROLLUP sseparatedbycommas.The
resultinggroupingsarethecrossproductofallthegroupsproducedbytheindividualgroupingsets.Itmightbealittleeasierto
understandwhatthismeansbylookingatanexample.Thefollowing GROUPINGSET resultsin2groupsofsubtotals,oneforthe
fact_1_id columnandoneforthe fact_id_2 column.
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 23/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_1_id,
fact_2_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id)ASgrouping_id
FROMdimension_tab
GROUPBYGROUPINGSETS(fact_1_id,fact_2_id)
ORDERBYfact_1_id,fact_2_id;
FACT_1_IDFACT_2_IDSALES_VALUEGROUPING_ID
124291.351
226237.041
110016.392
29377.782
310274.022
411324.122
59536.082
7rowsselected.
SQL>
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 24/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
SELECTfact_3_id,
fact_4_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_3_id,fact_4_id)ASgrouping_id
FROMdimension_tab
GROUPBYGROUPINGSETS(fact_3_id,fact_4_id)
ORDERBYfact_3_id,fact_4_id;
FACT_3_IDFACT_4_IDSALES_VALUEGROUPING_ID
16250.091
24702.231
35063.461
45139.231
55706.921
65282.751
74048.041
85311.591
94662.861
104361.221
14718.552
25439.12
34643.42
44515.32
55110.272
65910.782
74987.222
84846.252
95458.822
104898.72
20rowsselected.
SQL>
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 25/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
Ifwecombinethemtogetherintoaconcatenatedgroupingweget4groupsofsubtotals.Theoutputofthefollowingqueryisshown
here(concatenatedgroupings.txt).
SELECTfact_1_id,
fact_2_id,
fact_3_id,
fact_4_id,
SUM(sales_value)ASsales_value,
GROUPING_ID(fact_1_id,fact_2_id,fact_3_id,fact_4_id)ASgrouping_id
FROMdimension_tab
GROUPBYGROUPINGSETS(fact_1_id,fact_2_id),GROUPINGSETS(fact_3_id,fact_4_id)
ORDERBYfact_1_id,fact_2_id,fact_3_id,fact_4_id;
Theoutputfromthepreviousthreequeriesproducethefollowinggroupings.
GROUPINGSETS(fact_1_id,fact_2_id)
(fact_1_id)
(fact_2_id)
GROUPINGSETS(fact_3_id,fact_4_id)
(fact_3_id)
(fact_4_id)
GROUPINGSETS(fact_1_id,fact_2_id),GROUPINGSETS(fact_3_id,fact_4_id)
(fact_1_id,fact_3_id)
(fact_1_id,fact_4_id)
(fact_2_id,fact_3_id)
(fact_2_id,fact_4_id)
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 26/27
3/24/2017 ORACLEBASEROLLUP,CUBE,GROUPINGFunctionsandGROUPINGSETS
GROUPINGSETS(a,b),GROUPINGSETS(c,d)
(a,c)
(a,d)
(b,c)
(b,d)
Formoreinformationsee:
GROUPBY,ROLLUPandCUBEinOracle(https://www.youtube.com/watch?v=CCm4IYNtfw)
SQLforAggregationinDataWarehouses(http://docs.oracle.com/cd/E11882_01/server.112/e25554/aggreg.htm)
Hopethishelps.RegardsTim...
BacktotheTop.
18comments,read/addthem...(/misc/comments?page_id=940)
Home(/)|Articles(/articles/articles)|Scripts(/dba/scripts)|Blog(/blog/)|Certification(/misc/ocpcertification)|Misc
(/misc/miscellaneous)|About(/misc/siteinfo)
AboutTimHall(/misc/siteinfo#biog)
Copyright&Disclaimer(/misc/siteinfo#copyright)
Translate
https://oraclebase.com/articles/misc/rollupcubegroupingfunctionsandgroupingsets#grouping_functions 27/27