7.2.4. GROUPING SETS, CUBE, and ROLLUP

7.2.4. GROUPING SETSCUBE, and ROLLUP
7.2.4.GROUPING SETS, CUBEROLLUP
More complex grouping operations than those described above are possible using the concept of grouping sets. The data selected by the FROM and WHERE clauses is grouped separately by each specified grouping set, aggregates computed for each group just as for simple GROUP BY clauses, and then the results returned. For example:
使用分组集的概念可以实现比上述更复杂的分组操作。由FROM和WHERE子句选择的数据按每个指定的分组集分别分组,像简单的GROUP BY子句一样为每个组计算聚合,然后返回结果。 例如:
 
=> SELECT * FROM items_sold;
brand | size | sales
-------+------+-------
Foo | L | 10
Foo | M | 20
Bar | M | 15
Bar | L | 5
(4 rows)
=> SELECT brand, size, sum(sales) FROM items_sold GROUP BY GROUPING
SETS ((brand), (size), ());
brand | size | sum
-------+------+-----
Foo   |      | 30
Bar   |      | 20
      | L    | 15
      | M    | 35
      |      | 50
(5 rows)
 
Each sublist of GROUPING SETS may specify zero or more columns or expressions and is interpreted the same way as though it were directly in the GROUP BY clause. An empty grouping set means that all rows are aggregated down to a single group (which is output even if no input rows were present), as described above for the case of aggregate functions with no GROUP BY clause.
GROUPING SETS的每个子列表都可以指定零个或多个列或表达式,并且其解释方式与直接位于GROUP BY子句中的方式相同。 空分组集意味着所有行都被汇总为一个组(即使不存在输入行也将被输出),如上面针对不具有GROUP BY子句的聚合函数的情况。
 
References to the grouping columns or expressions are replaced by null values in result rows for grouping sets in which those columns do not appear. To distinguish which grouping a particular output row resulted from, see Table 9.56.
在没有出现这些列的分组集的结果行中,对分组列或表达式的引用将替换为空值。要区分特定输出行是来自哪个分组,请参见表9.56。
uploading.4e448015.gif转存失败重新上传取消
 
A shorthand notation is provided for specifying two common types of grouping set. A clause of the form
提供了一种速记符号,用于指定两种常见的分组集类型。 该子句的形式:
 
ROLLUP ( e1, e2, e3, ... )
 
represents the given list of expressions and all prefixes of the list including the empty list; thus it is equivalent to
表示给定的表达式列表以及列表的所有前缀,包括空列表; 因此,它等效于
 
GROUPING SETS (
( e1, e2, e3, ... ),
...
( e1, e2 ),
( e1 ),
( )
)
 
This is commonly used for analysis over hierarchical data; e.g. total salary by department, division,and company-wide total.
这通常用于对分层数据进行分析。 例如按小组,部门和公司范围计算总工资。
 
A clause of the form
以下子句格式:
 
CUBE ( e1, e2, ... )
 
represents the given list and all of its possible subsets (i.e. the power set). Thus
表示给定列表及其所有可能的子集。因此:
 
CUBE ( a, b, c )
 
is equivalent to
等价于:
 
GROUPING SETS (
( a, b, c ),
( a, b ),
( a, c ),
( a ),
( b, c ),
( b ),
( c ),
( )
)
The individual elements of a CUBE or ROLLUP clause may be either individual expressions, or sublists of elements in parentheses. In the latter case, the sublists are treated as single units for the purposes of generating the individual grouping sets. For example:
CUBE或ROLLUP子句的单个元素可以是单个表达式,也可以是括号中元素的子列表。 在后一种情况下,出于生成单个分组集的目的,子列表被视为单个单元。 例如:
 
CUBE ( (a, b), (c, d) )
 
is equivalent to
等价于:
 
GROUPING SETS (
( a, b, c, d ),
( a, b ),
( c, d ),
( )
)
 
and
而且:
 
ROLLUP ( a, (b, c), d )
 
is equivalent to
等效于:
 
GROUPING SETS (
( a, b, c, d ),
( a, b, c ),
( a ),
( )
)
 
The CUBE and ROLLUP constructs can be used either directly in the GROUP BY clause, or nested inside a GROUPING SETS clause. If one GROUPING SETS clause is nested inside another, the effect is the same as if all the elements of the inner clause had been written directly in the outer clause. 
CUBE和ROLLUP构造既可以直接在GROUP BY子句中使用,也可以嵌套在GROUPING SETS子句中。如果一个GROUPING SETS子句嵌套在另一个子句中,则效果与将内部子句的所有元素直接写入外部子句的效果相同。
 
If multiple grouping items are specified in a single GROUP BY clause, then the final list of grouping sets is the cross product of the individual items. For example:
如果在单个GROUP BY子句中指定了多个分组项,则分组集的最终列表是各个项目的交叉组合。 例如:
 
GROUP BY a, CUBE (b, c), GROUPING SETS ((d), (e))
 
is equivalent to
等效于:
 
GROUP BY GROUPING SETS (
(a, b, c, d), (a, b, c, e),
(a, b, d), (a, b, e),
(a, c, d), (a, c, e),
(a, d), (a, e)
)
 
Note
The construct (a, b) is normally recognized in expressions as a row constructor. Within the GROUP BY clause, this does not apply at the top levels of expressions, and (a, b) is parsed as a list of expressions as described above. If for some reason you need a row constructor in a grouping expression, use ROW(a, b).
通常在表达式中将构造(a,b)识别为行构造器。 在GROUP BY子句中,这不适用于表达式的顶层,并且如上所述,(a,b)会解析为表达式列表。 如果出于某种原因在分组表达式中需要行构造函数,请使用ROW(a,b)。
 
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104537036