When to use GROUPING SETS, CUBE and ROLLUP

前端 未结 3 1056
盖世英雄少女心
盖世英雄少女心 2021-01-30 22:08

I have recently learned about GROUPING SETS, CUBE and ROLLUP for defining multiple grouping sets in sql server.

What I am asking is under what circumstances do we use th

3条回答
  •  生来不讨喜
    2021-01-30 22:44

    The CUBE is the same of GROUPING SETS with all possible combinations.

    So this (using CUBE)

    GROUP BY CUBE (C1, C2, C3, ..., Cn-2, Cn-1, Cn)
    

    is the same of this (using GROUPING SETS)

    GROUP BY GROUPING SETS (
         (C1, C2, C3, ..., Cn-2, Cn-1, Cn) -- All dimensions are included.
        ,( , C2, C3, ..., Cn-2, Cn-1, Cn) -- n-1 dimensions are included.
        ,(C1, C3, ..., Cn-2, Cn-1, Cn)
        …
        ,(C1, C2, C3, ..., Cn-2, Cn-1,)
        ,(C3, ..., Cn-2, Cn-1, Cn) -- n-2 dimensions included
        ,(C1  ..., Cn-2, Cn-1, Cn)
        …
        ,(C1, C2) -- 2 dimensions are included.
        ,…
        ,(C1, Cn)
        ,…
        ,(Cn-1, Cn)
        ,…
        ,(C1) -- 1 dimension included
        ,(C2)
        ,…
        ,(Cn-1)
        ,(Cn)
        ,() ) -- Grand total, 0 dimension is included.
    

    Then, if you don't really need all combinations, you should use GROUPING SETS rather than CUBE

    ROLLUP and CUBE operators generate some of the same result sets and perform some of the same calculations as OLAP applications. The CUBE operator generates a result set that can be used for cross tabulation reports. A ROLLUP operation can calculate the equivalent of an OLAP dimension or hierarchy.

    Look here to see Grouping Sets Equivalents


    UPDATE

    I think an example would help here. Suppose you have a table of number of UFOs sightings by country and gender, like bellow:

    ╔═════════╦═══════╦═════════╗
    ║ COUNTRY ║ GENDER║ #SIGHTS ║
    ╠═════════╬═══════╬═════════╣
    ║ USA     ║ F     ║     450 ║
    ║ USA     ║ M     ║    1500 ║
    ║ ITALY   ║ F     ║     704 ║
    ║ ITALY   ║ M     ║     720 ║
    ║ SWEDEN  ║ F     ║     317 ║
    ║ SWEDEN  ║ M     ║     310 ║
    ║ BRAZIL  ║ F     ║     144 ║
    ║ BRAZIL  ║ M     ║     159 ║
    ╚═════════╩═══════╩═════════╝
    

    Then, if you want to know the totals for each country, by gender and grand total only, you should use GROUPING SETS

     select Country, Gender, sum(Number_Of_Sights)
     from Table1
     group by GROUPING SETS((Country), (Gender), ())
     order by Country, Gender
    

    SQL Fiddle

    To get the same result with GROUP BY, you would use UNION ALL as:

    select Country, NULL Gender, sum(Number_Of_Sights)
    from Table1
    GROUP BY Country
    UNION ALL
    select NULL Country, Gender, sum(Number_Of_Sights)
    from Table1
    GROUP BY GENDER
    UNION ALL
    SELECT NULL Country, NULL Gender, sum(Number_Of_Sights)
    FROM TABLE1
    ORDER BY COUNTRY, GENDER
    

    SQL Fiddle

    But it is not possible to obtain the same result with CUBE, since it will return all possibilities.

    Now, if you want to know all possible combinations, then you should use CUBE

提交回复
热议问题