当前位置:网站首页>Example of using gbase 8A OLAP function group by grouping sets
Example of using gbase 8A OLAP function group by grouping sets
2022-06-27 21:52:00 【The source of life;】
GBase 8a Of group grouping sets function , For those in brackets n Each field or expression is done GROUP BY operation , Finally, combine the results . Equivalent to multiple separate group by Then make another one union all operation .
grammar
GROUP BY GROUPING SETS( (…),(…),…)
explain
GROUP BY GROUPING SETS (A,B,C) (A、B、C Stands for... In grammar “(…)”)
First of all, (A) Conduct GROUP BY, Then on (B) Conduct GROUP BY, And then we do GROUP BY, Finally, combine all the results together ( amount to UNION ALL operation ), If n One or more of the fields or expressions do not appear in a group GROUP BY Back , use NULL Replace fields or expressions that do not appear .
Examples
Sample data
Simple student subject examination records .
gbase> select * from score;
+--------+---------+-------+
| name | subject | score |
+--------+---------+-------+
| Wang hai | Chinese language and literature | 86 |
| Wang hai | mathematics | 83 |
| Wang hai | English | 93 |
| Tao Jun | Chinese language and literature | 88 |
| Tao Jun | mathematics | 84 |
| Tao Jun | English | 94 |
| Liu Ke | Chinese language and literature | 80 |
| Liu Ke | mathematics | 86 |
| Liu Ke | English | 88 |
| Li Chun | Chinese language and literature | 89 |
| Li Chun | mathematics | 80 |
| Li Chun | English | 87 |
+--------+---------+-------+
12 rows in set (Elapsed: 00:00:00.01)
Get the summary results of students and subjects
gbase> select name,subject,sum(score) from score group by grouping sets(name,subject);
+--------+---------+------------+
| name | subject | sum(score) |
+--------+---------+------------+
| NULL | Chinese language and literature | 343 |
| NULL | mathematics | 333 |
| NULL | English | 362 |
| Wang hai | NULL | 262 |
| Tao Jun | NULL | 266 |
| Liu Ke | NULL | 254 |
| Li Chun | NULL | 256 |
+--------+---------+------------+
7 rows in set (Elapsed: 00:00:00.02)
Multiple Sets In between, too union all Relationship . This example simply converts the previous use case's 2 Conditions , Split up . The result is the same .
gbase> select name,subject,sum(score) from score group by grouping sets((name),(subject));
+--------+---------+------------+
| name | subject | sum(score) |
+--------+---------+------------+
| NULL | Chinese language and literature | 343 |
| NULL | mathematics | 333 |
| NULL | English | 362 |
| Wang hai | NULL | 262 |
| Tao Jun | NULL | 266 |
| Liu Ke | NULL | 254 |
| Li Chun | 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) |
+--------+---------+------------+
| Wang hai | Chinese language and literature | 86 |
| Wang hai | mathematics | 83 |
| Wang hai | English | 93 |
| Tao Jun | Chinese language and literature | 88 |
| Tao Jun | mathematics | 84 |
| Tao Jun | English | 94 |
| Liu Ke | Chinese language and literature | 80 |
| Liu Ke | mathematics | 86 |
| Liu Ke | English | 88 |
| Li Chun | Chinese language and literature | 89 |
| Li Chun | mathematics | 80 |
| Li Chun | English | 87 |
| Wang hai | NULL | 262 |
| Tao Jun | NULL | 266 |
| Liu Ke | NULL | 254 |
| Li Chun | NULL | 256 |
+--------+---------+------------+
16 rows in set (Elapsed: 00:00:00.04)
In the second example above , The first part is name, The second part is (name,subject) Two fields together .
Be careful , Don't think about repetition , That is, you can write more name.
gbase> select name,subject,sum(score) from score group by grouping sets(name,name,subject);
+--------+---------+------------+
| name | subject | sum(score) |
+--------+---------+------------+
| NULL | Chinese language and literature | 343 |
| NULL | mathematics | 333 |
| NULL | English | 362 |
| Wang hai | NULL | 262 |
| Tao Jun | NULL | 266 |
| Liu Ke | NULL | 254 |
| Li Chun | NULL | 256 |
| Wang hai | NULL | 262 |
| Tao Jun | NULL | 266 |
| Liu Ke | NULL | 254 |
| Li Chun | NULL | 256 |
+--------+---------+------------+
11 rows in set (Elapsed: 00:00:00.03)
边栏推荐
- The difference between scrum and Kanban
- [LeetCode]30. Concatenate substrings of all words
- [LeetCode]100. 相同的树
- [LeetCode]513. 找树左下角的值
- Null pointer exception
- 100 important knowledge points that SQL must master: using functions to process data
- Go从入门到实战——仅执行一次(笔记)
- What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
- excel读取文件内容方法
- [leetcode] dynamic programming solution split integer i[silver fox]
猜你喜欢

畅游动态规划之区间DP

Simulink导出FMU模型文件方法

∫(0→1) ln(1+x) / (x ² + 1) dx

语言弱点列表--CWE,一个值得学习的网站

Codeforces Round #723 (Div. 2)

Go从入门到实战——channel的关闭和广播(笔记)

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

C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。

List of language weaknesses --cwe, a website worth learning

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
随机推荐
JVM memory structure when creating objects
∫(0→1) ln(1+x) / (x² + 1) dx
How to participate in openharmony code contribution
GBase 8a数据库用户密码安全相关参数汇总
[LeetCode]动态规划解拆分整数I[Silver Fox]
流程控制任务
Process control task
win11桌面出现“了解此图片”如何删除
小王的面试训练任务
GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
动态刷新mapper看过来
Golang 使用正则来匹配出子字符串函数
IO stream code
[LeetCode]572. 另一棵树的子树
Quick excel export according to customized excel Title Template
Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
Little known MySQL import data
空指针异常
Special training of guessing game
oracle迁移mysql唯一索引大小写不区分别怕