当前位置:网站首页>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)
边栏推荐
猜你喜欢
![[leetcode] dynamic programming solution partition array i[red fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[leetcode] dynamic programming solution partition array i[red fox]

快递e栈——数组篇小型项目

空指针异常

开源技术交流丨一站式全自动化运维管家ChengYing入门介绍

JVM memory structure when creating objects
![[leetcode] dynamic programming solution split integer i[silver fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[leetcode] dynamic programming solution split integer i[silver fox]

PCIE知识点-008:PCIE switch的结构

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

Go从入门到实战——接口(笔记)

vmware虚拟机PE启动
随机推荐
String类的常用方法
跟我一起AQS SOS AQS
C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
[LeetCode]30. Concatenate substrings of all words
[leetcode] 508. Élément de sous - arbre le plus fréquent et
Null pointer exception
石子合并问题分析
Educational Codeforces Round 108 (Rated for Div. 2)
Go from introduction to practice - polymorphism (note)
Go从入门到实战——共享内存并发机制(笔记)
How to delete "know this picture" on win11 desktop
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
[leetcode] dynamic programming solution split integer i[silver fox]
Simulink导出FMU模型文件方法
Set code exercise
Go从入门到实战——所有任务完成(笔记)
Go from introduction to actual combat -- channel closing and broadcasting (notes)
qt base64加解密
畅游动态规划之区间DP
Go from entry to practice -- CSP concurrency mechanism (note)