PG:什么是grouping sets

简介: PG:什么是grouping sets

学习PG聚合操作机制时,看到ExecInitAgg中初始化时有对AggState中有pergroups等进行初始化[AggState的解释见上一篇:GreenPlum7聚合操作结构体之间关系

https://mp.weixin.qq.com/s?__biz=MzU1OTgxMjA4OA==&mid=2247484647&idx=1&sn=6ad81bfe9842bcba9ef3603efc18d14f&chksm=fc10d870cb675166f89a179e18fd2b56a343fe13234e1354b478c386935f890207d70c64c407&token=149355293&lang=zh_CN#rd]

那么什么是pergroups呢,它的作用是什么?

ExecInitAgg中:可以看到组数等于组集合数(若没hash的话),每个分组中有numaggs个聚集

这里就需要理解什么是grouping sets。

PostgreSQL9.5版本引入了group by的分组集合功能,提供了GROUPING SETSCUBEROLLUP参数。这些新的标准SQL选项可以让用户在一次查询中生成多个不同级别或者多维度的报表,而不是像以前一样需要几次查询。

    postgres=# create table t1(id1 int,name varchar(20),class int,score int);
    CREATE TABLE
    postgres=# select * from t1;
     id1 |   name   | class | score
    -----+----------+-------+-------
       1 | math     |     1 |    50
       2 | math     |     2 |    90
       3 | math     |     1 |    70
       5 | chinese  |     1 |    50
       6 | chinese  |     2 |    60
       4 | chinese  |     2 |    60
       7 | physical |     1 |    90
       8 | physical |     2 |    80
    (8 行记录)

    首先看下普通的group by:根据nameclass字段求和:

    可以看到到仅一个聚合分类。那么再看下GROUPING SETS语法:

    3种聚合分类:1)针对name进行分组然后求sum值;2)针对class进行分组,然后求sum值;3grouping sets种的空,表示所有行都聚合到一个分组中。

    需要注意,grouping sets的集合中,一个括号一个分组,如下(name,class)为一个分组。该语句等价于普通的group by

    总结:grouping sets中有一个集合,则有一个聚合;若有2个集合,则2个聚合。


    目录
    相关文章
    |
    7月前
    |
    JSON 关系型数据库 MySQL
    EXPLAIN Join Types
    `EXPLAIN` 输出的 `type` 列描述了表连接方式,从最优到最差包括:`system`(单行系统表)、`const`(最多一行,视为常量)、`eq_ref`(最佳连接类型,用于主键或唯一索引)、`ref`(基于索引的部分匹配)、`fulltext`(全文索引)、`ref_or_null`(包含 NULL 值的行)、`index_merge`(索引合并优化)、`unique_subquery` 和 `index_subquery`(索引查找替代子查询)、`range`(索引范围内检索)、`index`(索引扫描)、`ALL`(全表扫描,通常最差)。
    |
    SQL 分布式计算 Spark
    SPARK Expand问题的解决(由count distinct、group sets、cube、rollup引起的)
    SPARK Expand问题的解决(由count distinct、group sets、cube、rollup引起的)
    821 0
    SPARK Expand问题的解决(由count distinct、group sets、cube、rollup引起的)
    |
    数据库
    解决which is not functionally dependent on columns in GROUP BY clause;...sql_mode=only_full_group_by
    解决which is not functionally dependent on columns in GROUP BY clause;...sql_mode=only_full_group_by
    396 0
    |
    SQL 关系型数据库 数据库
    PG/GP group by expression语法
    PG/GP group by expression语法
    145 1
    |
    存储 SQL 关系型数据库
    1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column的解决办法
    1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column的解决办法
    529 0
    1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
    1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
    228 0
    1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
    |
    SQL 关系型数据库 MySQL
    【已解决】SELECT list is not in GROUP BY clause and contains nonaggregated column
    MySQL5.7.5后only_full_group_by成为sql_mode的默认选项之一,这可能导致一些sql语句失效。
    611 0
    |
    SQL 关系型数据库 MySQL
    MySQL - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
    MySQL - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
    389 0
    |
    SQL 关系型数据库 MySQL
    Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
    Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
    432 0
    AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等