当前位置:网站首页>GBase 8a OLAP函数group by grouping sets的使用样例
GBase 8a OLAP函数group by grouping sets的使用样例
2022-06-27 19:33:00 【生命之源;】
GBase 8a的group grouping sets功能,对后面括号里的 n 个字段或表达式分别做 GROUP BY 操作,最后将结果合并在一起。等同于多个单独的group by后再做一个union all操作。
语法
GROUP BY GROUPING SETS( (…),(…),…)
说明
GROUP BY GROUPING SETS (A,B,C) (A、B、C 代表语法中的“(…)”)
首先对(A)进行 GROUP BY,然后对(B)进行 GROUP BY,然后对进行 GROUP BY,最后将所有结果合并在一起(相当于 UNION ALL 操作),如果 n 个字段或表达式中的一个或多个在某一分组中不出现在 GROUP BY 后面,用 NULL 代替不出现的字段或表达式。
样例
数据样例
简单的学生科目考试记录。
gbase> select * from score;
+--------+---------+-------+
| name | subject | score |
+--------+---------+-------+
| 王海 | 语文 | 86 |
| 王海 | 数学 | 83 |
| 王海 | 英语 | 93 |
| 陶俊 | 语文 | 88 |
| 陶俊 | 数学 | 84 |
| 陶俊 | 英语 | 94 |
| 刘可 | 语文 | 80 |
| 刘可 | 数学 | 86 |
| 刘可 | 英语 | 88 |
| 李春 | 语文 | 89 |
| 李春 | 数学 | 80 |
| 李春 | 英语 | 87 |
+--------+---------+-------+
12 rows in set (Elapsed: 00:00:00.01)
获得学生和学科各自的汇总结果
gbase> select name,subject,sum(score) from score group by grouping sets(name,subject);
+--------+---------+------------+
| name | subject | sum(score) |
+--------+---------+------------+
| NULL | 语文 | 343 |
| NULL | 数学 | 333 |
| NULL | 英语 | 362 |
| 王海 | NULL | 262 |
| 陶俊 | NULL | 266 |
| 刘可 | NULL | 254 |
| 李春 | NULL | 256 |
+--------+---------+------------+
7 rows in set (Elapsed: 00:00:00.02)
多个Sets之间也是union all关系。本例就简单的将上一个用例的2个条件,拆分一下。结果是一样的。
gbase> select name,subject,sum(score) from score group by grouping sets((name),(subject));
+--------+---------+------------+
| name | subject | sum(score) |
+--------+---------+------------+
| NULL | 语文 | 343 |
| NULL | 数学 | 333 |
| NULL | 英语 | 362 |
| 王海 | NULL | 262 |
| 陶俊 | NULL | 266 |
| 刘可 | NULL | 254 |
| 李春 | NULL | 256 |
+--------+---------+------------+
7 rows in set (Elapsed: 00:00:00.02)
gbase> select name,subject,sum(score) from score group by grouping sets(name,(name,subject));
+--------+---------+------------+
| name | subject | sum(score) |
+--------+---------+------------+
| 王海 | 语文 | 86 |
| 王海 | 数学 | 83 |
| 王海 | 英语 | 93 |
| 陶俊 | 语文 | 88 |
| 陶俊 | 数学 | 84 |
| 陶俊 | 英语 | 94 |
| 刘可 | 语文 | 80 |
| 刘可 | 数学 | 86 |
| 刘可 | 英语 | 88 |
| 李春 | 语文 | 89 |
| 李春 | 数学 | 80 |
| 李春 | 英语 | 87 |
| 王海 | NULL | 262 |
| 陶俊 | NULL | 266 |
| 刘可 | NULL | 254 |
| 李春 | NULL | 256 |
+--------+---------+------------+
16 rows in set (Elapsed: 00:00:00.04)
如上第二个例子中,第一部分是name, 第二部分是(name,subject)两个字段一起。
注意,不考虑重复,也就是你可以写多个name。
gbase> select name,subject,sum(score) from score group by grouping sets(name,name,subject);
+--------+---------+------------+
| name | subject | sum(score) |
+--------+---------+------------+
| NULL | 语文 | 343 |
| NULL | 数学 | 333 |
| NULL | 英语 | 362 |
| 王海 | NULL | 262 |
| 陶俊 | NULL | 266 |
| 刘可 | NULL | 254 |
| 李春 | NULL | 256 |
| 王海 | NULL | 262 |
| 陶俊 | NULL | 266 |
| 刘可 | NULL | 254 |
| 李春 | NULL | 256 |
+--------+---------+------------+
11 rows in set (Elapsed: 00:00:00.03)
边栏推荐
- Go from starting to Real - Interface (note)
- JVM memory structure when creating objects
- 动态刷新mapper看过来
- IO stream code
- 100 important knowledge points that SQL must master: creating calculation fields
- Go从入门到实战——共享内存并发机制(笔记)
- MySQL performance optimization index function, hidden, prefix, hash index usage (2)
- Ceph分布式存储
- Modify large online games through CE modifier
- win11桌面出现“了解此图片”如何删除
猜你喜欢

Go from introduction to actual combat - context and task cancellation (notes)

Go from introduction to actual combat -- channel closing and broadcasting (notes)

富文本 考试 填空题

100 important knowledge points that SQL must master: sorting and retrieving data

White whoring red team goby & POC, how do you call white whoring?

Icml2022 | scalable depth Gaussian Markov random field

SQL必需掌握的100个重要知识点:排序检索数据

Ceph分布式存储

Industry case | see the operation of bank digital transformation from the king of retail

让马化腾失望了!Web3.0,毫无希望
随机推荐
Day8 ---- 云资讯项目介绍与创建
How to participate in openharmony code contribution
PCIE知识点-008:PCIE switch的结构
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Go from introduction to actual combat -- channel closing and broadcasting (notes)
mysql使用笔记一
Use the storcli tool to configure raid. Just collect this article
AI 绘画极简教程
100 important knowledge points that SQL must master: filtering data
如何将队列里面的内容没秒钟执行一次优先级
GFS distributed file system
体验Navicat Premium 16,无限重置试用14天方法(附源码)
After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum
专题教程——选队长游戏
鲜为人知的mysql导入数据
Go from introduction to actual combat - all tasks completed (notes)
Codeforces Round #723 (Div. 2)
Here are 12 commonly used function formulas for you. All used ones are good
Very comprehensive dolphin scheduler installation and use documents