CUBE 运算符在 SELECT 语句的 GROUP BY 子句中指定。该语句的选择列表应包含维度列和聚合函数表达式。GROUP BY 应指定维度列和关键字 WITH CUBE。结果集将包含维度列中各值的所有可能组合,以及与这些维度值组合相匹配的基础行中的聚合值。 =========================
CUBE 和 ROLLUP 之间的区别在于:
CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。
The ROLLUP operator is useful in generating reports that contain subtotals and totals. The ROLLUP operator generates a result set that is similar to the result sets generated by the CUBE operator.
The differences between CUBE and ROLLUP are:
CUBE generates a result set showing aggregates for all combinations of values in the selected columns.
ROLLUP generates a result set showing aggregates for a hierarchy of values in the selected columns.
最后查询语句如下:
SELECTCASEWHEN (GROUPING(ID) =1) THEN''''ALL'''' ELSEISNULL(ID, ''''UNKNOWN'''') ENDAS ID, CASEWHEN (GROUPING(Catalog) =1) THEN''''ALL'''' ELSEISNULL(Catalog, ''''UNKNOWN'''') ENDAS Catalog, SUM(Num) AS Num FROM Test GROUPBY ID, Catalog WITH ROLLUP