当前位置:网站首页>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)
边栏推荐
- Installing Oracle11g under Linux
- String类的常用方法
- Go from introduction to actual combat - package (notes)
- 石子合并问题分析
- [LeetCode]572. A subtree of another tree
- Go从入门到实战——错误机制(笔记)
- 有时间看看ognl表达式
- Matlab finds the position of a row or column in the matrix
- Method of reading file contents by Excel
- .NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
猜你喜欢

100 important knowledge points that SQL must master: using functions to process data

How to participate in openharmony code contribution

How to delete "know this picture" on win11 desktop

JVM memory structure when creating objects

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!"

AI painting minimalist tutorial

洛谷P5706 再分肥宅水

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

C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.

Go from starting to Real - Interface (note)
随机推荐
[LeetCode]572. 另一棵树的子树
石子合并问题分析
集合代码练习
[LeetCode]161. Edit distance of 1
Go from introduction to actual combat - panic and recover (notes)
win11桌面出现“了解此图片”如何删除
[LeetCode]100. 相同的树
JVM memory structure when creating objects
Golang 使用正则来匹配出子字符串函数
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Go from entry to practice - multiple selection and timeout control (notes)
IO stream code
Go 访问GBase 8a 数据库的一个方法
Quick excel export according to customized excel Title Template
01-Golang-环境搭建
TreeSet details
MYSQL和MongoDB的分析
[leetcode] 508. Élément de sous - arbre le plus fréquent et
Interval DP of Changyou dynamic programming
GBase 8a OLAP分析函数cume_dist的使用样例