企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
grouping sets 高级聚合本质是多重`group by`进行`union all`操作。 ```sql SELECT a,b,SUM(c) FROM tab1 GROUP BY a,b GROUPING SETS((a,b),a,b,()); -- 等价于 SELECT a,b,SUM(c) FROM tab1 GROUP BY a,b union SELECT a,null,SUM(c) FROM tab1 GROUP BY a,null union SELECT null,b,SUM(c) FROM tab1 GROUP BY null,b union SELECT null,null,SUM(c) FROM tab1; ``` :-: 表 GROUPING SETS 及等价语句 | GROUPING SETS 语句 | 等价于 GROUP BY 语句 | | --- | --- | | SELECT a, b, SUM(c) FROM tab1 <br/>GROUP BY a, b <br/>GROUPING SETS ((a, b), a, b, ( )); | SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b <br/> UNION <br/> SELECT a, null, SUM( c ) FROM tab1 GROUP BY a, null <br/> UNION <br/> SELECT null, b, SUM( c ) FROM tab1 GROUP BY null, b <br/> UNION <br/> SELECT null, null, SUM( c ) FROM tab1; | | SELECT a, b, SUM( c ) FROM tab1 <br/> GROUP BY a, b GROUPING SETS ( (a,b), a); | SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b <br/> UNION <br/> SELECT a, null, SUM( c ) FROM tab1 GROUP BY a; | | SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS ( (a,b) ); | SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b; | | SELECT a,b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS (a,b); | SELECT a, null, SUM( c ) FROM tab1 GROUP BY a <br/> UNION <br/> SELECT null, b, SUM( c ) FROM tab1 GROUP BY b; | **`GROUP BY WITH CUBE|ROLLUP`** >`CUBE`:对分组列进行所有可能组合的聚合,是 group by 字段的所有组合 `ROLLUP`:计算维度层次级别上的聚合 :-: 表 ROLLUP/CUBE 及等价语句 | ROLLUP/CUBE 语句 | 等价于 GROUP BY 语句 | | --- | --- | | SELECT <br/> a, b, SUM( c ) <br/> FROM tab1 <br/> GROUP BY a, b, c WITH CUBE; | SELECT <br/> a, b, SUM( c ) <br/> FROM tab1 <br/> GROUP BY a, b, c <br/> GROUPING SETS ( (a, b, c), (a, b), (b, c), (a, c), (a), (b), (c), ( )); | | SELECT <br/> a, b, SUM( c ) <br/> FROM tab1 <br/> GROUP BY a, b, c WITH ROLLUP; | SELECT <br/> a, b, SUM( c ) <br/> FROM tab1 <br/> GROUP BY a, b, c GROUPING SETS ( (a, b, c), (a, b), (a), ( )); |