PostgreSQL 分组集合新功能(GROUPING SETS,CUBE,ROLLUP)

实验环境

操作系统:windows 10 家庭中文版

数据库系统: PostgreSQL 9.6.2

说明

postgresql从9.5版本开始新加入了group by的分组集合功能,提供了GROUPING SETS,CUBE,ROLLUP参数,使用方式与oracle完全一致,下面是实际测试说明

构建测试环境

创建表t并插入测试数据:

create table t(id int,name varchar(20),class int,score int);

insert into t values(1,'math',1,90);

insert into t values(2,'math',2,80);

insert into t values(3,'math',1,70);

insert into t values(4,'chinese',2,60);

insert into t values(5,'chinese',1,50);

insert into t values(6,'chinese',2,60);

insert into t values(7,'physical',1,70);

insert into t values(8,'physical',2,80);

insert into t values(9,'physical',1,90);

结果:

test=# select * from t;

id | name | class | score

----+----------+-------+-------

1 | math | 1 | 90

2 | math | 2 | 80

3 | math | 1 | 70

4 | chinese | 2 | 60

5 | chinese | 1 | 50

6 | chinese | 2 | 60

7 | physical | 1 | 70

8 | physical | 2 | 80

9 | physical | 1 | 90

(9 行记录)

普通的group by

根据name和class字段求和:

test=# select name,class,sum(score)

test-# from t

test-# group by name,class

test-# order by name,class

test-# ;

name | class | sum

----------+-------+-----

chinese | 1 | 50

chinese | 2 | 120

math | 1 | 160

math | 2 | 80

physical | 1 | 160

physical | 2 | 80

(6 行记录)

grouping set

GROUPING SETS的每个子列表可以指定零个或多个列或表达式,并且与其直接在GROUP BY子句中的解释方式相同。 一个空的分组集合意味着所有的行都被聚合到一个组中(即使没有输入行存在,也是输出)。

test=# select name,class,sum(score)

test-# from t

test-# group by grouping sets((name),(class),())

test-# order by name,class

test-# ;

name | class | sum

----------+-------+-----

chinese | | 170

math | | 240

physical | | 240

| 1 | 370

| 2 | 280

| | 650

(6 行记录)

顺带一提,默认的group by语句相当于grouping set在grouping set后的参数填上所有group by的字段。如下:

test=# select name,class,sum(score)

test-# from t

test-# group by grouping sets((name,class))

test-# order by name,class

test-# ;

name | class | sum

----------+-------+-----

chinese | 1 | 50

chinese | 2 | 120

math | 1 | 160

math | 2 | 80

physical | 1 | 160

physical | 2 | 80

(6 行记录)

与不使用grouping set语句时的结果完全相同

rollup

* rollup((a),(b),(c))等价于grouping sets((a,b,c),(a,b),(a),()) *

test=# select name,class,sum(score)

test-# from t

test-# group by rollup((name),(class))

test-# order by name,class

test-# ;

name | class | sum

----------+-------+-----

chinese | 1 | 50

chinese | 2 | 120

chinese | | 170

math | 1 | 160

math | 2 | 80

math | | 240

physical | 1 | 160

physical | 2 | 80

physical | | 240

| | 650

(10 行记录)

等价于:

grouping sets((name,class),(name),())

cube

* cube((a),(b),(c))等价于grouping sets((a,b,c),(a,b),(a,c),(a),(b,c),(b),(c),()) *

test=# select name,class,sum(score)

test-# from t

test-# group by cube((name),(class))

test-# order by name,class

test-# ;

name | class | sum

----------+-------+-----

chinese | 1 | 50

chinese | 2 | 120

chinese | | 170

math | 1 | 160

math | 2 | 80

math | | 240

physical | 1 | 160

physical | 2 | 80

physical | | 240

| 1 | 370

| 2 | 280

| | 650

(12 行记录)

等价于:

grouping sets((name,class),(name),(class),())

实际应用

我遇到一个需求,需要在分组统计总和之外附加所有组的总和,命名为total:

test=# select coalesce(name,'total') as name,

test-# coalesce(class,0) as class,

test-# coalesce(sum(score),0) as sum_score,

test-# coalesce(round(avg(score),2),0) as avg_score

test-# from t

test-# group by grouping sets((name,class),())

test-# order by name,class

test-# ;

name | class | sum_score | avg_score

----------+-------+-----------+-----------

chinese | 1 | 50 | 50.00

chinese | 2 | 120 | 60.00

math | 1 | 160 | 80.00

math | 2 | 80 | 80.00

physical | 1 | 160 | 80.00

physical | 2 | 80 | 80.00

total | 0 | 650 | 72.22

(7 行记录)

postgresql 集合类型_PostgreSQL 分组集合新功能(GROUPING SETS,CUBE,ROLLUP)相关推荐

  1. redis的数据结构||1) 字符串类型2) 哈希类型3) 列表类型4) 集合类型 5) 有序集合类型详解

    2. 下载安装     1. 官网:https://redis.io     2. 中文网:http://www.redis.net.cn/     3. 解压直接可以使用:         * re ...

  2. python集合类型_Python 的集合(set)类型

    本文简单介绍了Python语言里set类型自带的方法,set类型表示元素和集合的从属关系(membership),和列表等序列相比,最大的特点是无序.可以跟数学里的集合概念完全对应起来. 目录 概念 ...

  3. Postgresql 9.3外部数据封装的新功能

    2019独角兽企业重金招聘Python工程师标准>>> 这周postgresql发布了9.3正式版,该版本包含可写的外部数据封装.数据页 checksums.快速的故障转移以及 st ...

  4. postgresql兴建用户_postgresql – 为什么允许新用户创建表?

    我想知道为什么允许新创建的用户在连接到数据库后创建表.我有一个数据库,project2_core: postgres=# \l List of databases Name | Owner | Enc ...

  5. Swift中文教程(四) 集合类型

    Swift 提供两种集合类型来存储集合,数组和字典.数组是一个同类型的序列化列表集合.字典是一个能够使用类似于键的唯一标识符来获取值的非序列化集合. 在Swift中,数组和字典的键和值都必须明确它的类 ...

  6. Redis应用(五)---集合类型

    介绍 Redis支持两种集合分别是有序集合和无序集合 集合和列表之间的对比 集合类型 列表类型 有序集合类型 存储内容 最多 2 32 − 1 2 ^ {32} - 1 232−1个字符串 最多 2 ...

  7. jtoken判断是否包含键_Redis源码解析十三--有序集合类型键实现(t_zset)

    有序集合类型键实现 1. 有序集合命令 Redis有序集合命令如下表所示:Redis 有序集合命令详解 2. 有序集合类型实现 有序集合对象的底层实现类型如下表: 关于底层的数据结构剖析和实现,请看如 ...

  8. Python集合类型详解(一)——集合定义与集合操作符

    今天继续给大家介绍Python相关知识,本文主要内容是Python集合类型定义与集合操作符. 一.集合类型定义 在Python中,集合是一种非常重要的组合数据类型.Python中的集合与数学中的集合非 ...

  9. oracle集合类型详解

    1.集合类型 1.1关联数组 1.2嵌套表 1.3 可变长的数组 2.声明集合类型 2.1声明关联数组 2.2声明嵌套表 2.3声明VARRAY 3.集合变量的声明和初始化 3.1集合变量的声明 3. ...

最新文章

  1. 中国食用香精行业品牌策略与销售渠道分析报告2022-2028年
  2. linux内核在什么目录结构,Linux Kernel 目录结构说明
  3. CentOS curses 中文乱码问题
  4. ActionContextCleanUp作用
  5. AUTOSAR从入门到精通100讲(四十)-嵌入式中的CAN总线
  6. Java面试题:IO流的标准处理异常的代码
  7. java 不允许默认构造_java – 如何使用ObjectMapper去除/序列化不可变对象而不使用默认构造函数?...
  8. 移动端开发 main.js入口文件
  9. windows下sublime2 clojure环境配置
  10. 各位数字之和——一个神奇的结论
  11. 解决Ubuntu 14.04下CodeBlocks缩进异常问题
  12. mldn出品java风暴_MLDN出品JAVA风暴-JAVA学习的终极资料
  13. shp、tif文件坐标系转换
  14. 你好世界在Java语言中的编程代码
  15. 模拟IC芯片设计开发的流程
  16. 婴儿围栏ASTM F406亚马逊CPC美国CPSIA认证玩具CPC认证儿童产品CPC认证
  17. 在桌面上显示IP地址等信息
  18. [jdk]jdk7,jdk8,jdk14 linux版本,windows版本下载
  19. 【推荐架构day7】爱奇异推荐算法的演进之路
  20. ibm x3650 m2服务器维修手册,ibm x3650 m2服务器的详细配置资料.doc

热门文章

  1. Linux 之父怒删工程师提交的补丁,称“太蠢了”网友:怼得好!
  2. 和付费网盘说再见,自己起个网盘不香吗?| Java 开源项目
  3. 如何快速写一款小而美的“上滑无限加载的控件”?| 博文精选
  4. 扩容效率提升10倍,腾讯云发布一站式资源运维利器TIC
  5. 收好这份来自大厂技术大咖的“远程办公指南”
  6. 快速用 Haskell 构建超级简单的 Web 技术栈!
  7. 你有进一步深入理解二分查找吗?
  8. 300 秒带你吃透大数据!
  9. 华为正式发布自有操作系统——鸿蒙 OS!
  10. 2019,国产手机生死存亡的一年