当前位置:网站首页>GBase 8a OLAP分析函数 cume_dist的使用样例
GBase 8a OLAP分析函数 cume_dist的使用样例
2022-06-27 19:33:00 【生命之源;】
GBase 8a支持cume_dist 函数,用于计算小于等于或者大于等于(根据order的顺序)该值的百分比。
语法
cume_disk() over([partition by ] order by [desc])
说明
其中partition是否开窗,否则全部数值统一处理。
order by 的顺序,ASC(默认) = 小于等于,DESC =大于等于
样例
小于等于
如下例子是默认的升序,第一行表示:小于等于数值1的行数比例为20%
gbase> select id,cume_dist()over(order by id) cr from t2;
+------+-----+
| id | cr |
+------+-----+
| 1 | 0.2 |
| 2 | 0.4 |
| 3 | 0.6 |
| 4 | 0.8 |
| 5 | 1 |
+------+-----+
5 rows in set (Elapsed: 00:00:00.01)
大于等于
排序为desc,第一行表示:大于等于数值5的行数比例为20%。
gbase> select id,cume_dist()over(order by id desc) cr from t2;
+------+-----+
| id | cr |
+------+-----+
| 5 | 0.2 |
| 4 | 0.4 |
| 3 | 0.6 |
| 2 | 0.8 |
| 1 | 1 |
+------+-----+
5 rows in set (Elapsed: 00:00:00.02)
带开窗partition
每个partition内不分别计算百分比。
gbase> select * from t4;
+------+------+
| id | type |
+------+------+
| 1 | A |
| 2 | A |
| 3 | A |
| 1 | B |
| 2 | B |
| 3 | B |
| 4 | B |
+------+------+
7 rows in set (Elapsed: 00:00:00.00)
gbase> select type,id,cume_dist()over(partition by type order by id) cr from t4;
+------+------+-------------------+
| type | id | cr |
+------+------+-------------------+
| A | 1 | 0.333333333333333 |
| A | 2 | 0.666666666666667 |
| A | 3 | 1 |
| B | 1 | 0.25 |
| B | 2 | 0.5 |
| B | 3 | 0.75 |
| B | 4 | 1 |
+------+------+-------------------+
7 rows in set (Elapsed: 00:00:00.07)
与Percent_rank的对比
percent_rank是计算相对位置,包含起点0,而cume_dist是包含等于的,所以不会出现0。如果数据只有1行,那么percent_rank为几点0, 而cume_dist为1(100%)。
gbase> select id,cume_dist()over(order by id) cr,percent_rank()over(order by id) pr from t2;
+------+-----+------+
| id | cr | pr |
+------+-----+------+
| 1 | 0.2 | 0 |
| 2 | 0.4 | 0.25 |
| 3 | 0.6 | 0.5 |
| 4 | 0.8 | 0.75 |
| 5 | 1 | 1 |
+------+-----+------+
5 rows in set (Elapsed: 00:00:00.01)
一行数据
gbase> select id,cume_dist()over(order by id) cr,percent_rank()over(order by id) pr from t5;
+------+----+----+
| id | cr | pr |
+------+----+----+
| 1 | 1 | 0 |
+------+----+----+
1 row in set (Elapsed: 00:00:00.02)
边栏推荐
- 集合代码练习
- 100 important knowledge points that SQL must master: sorting and retrieving data
- Go从入门到实战——任务的取消(笔记)
- 跟我一起AQS SOS AQS
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- Can Oracle's CTAs bring constraints and other attributes to the new table?
- TreeSet详解
- Oracle的CTAS能不能将约束等属性带到新表?
- Go从入门到实战——Panic和recover(笔记)
- Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
猜你喜欢

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献

Let Ma Huateng down! Web3.0, hopeless

Go从入门到实战—— 多路选择和超时控制(笔记)

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

Codeforces Round #716 (Div. 2)

Special training of guessing game
![Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion](/img/21/9c5f5122270adea9444ff5f2d199ed.jpg)
Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion

Focus! Tips for installing fonts on domestic computers

Go从入门到实战——Panic和recover(笔记)

Go从入门到实战——CSP并发机制(笔记)
随机推荐
100 important knowledge points that SQL must master: filtering data
创建对象时JVM内存结构
Go从入门到实战——多态(笔记)
gomock mockgen : unknown embedded interface
GoLand permanently activated
Here are 12 commonly used function formulas for you. All used ones are good
AI 绘画极简教程
Educational Codeforces Round 108 (Rated for Div. 2)
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!"
Industry case | see the operation of bank digital transformation from the king of retail
Save method of JPA stepping pit series
Scrum和看板的区别
DO280OpenShift访问控制--security policy和章节实验
系统自带的karsonzhang/fastadmin-addons报错
Go从入门到实战——协程机制(笔记)
Use the storcli tool to configure raid. Just collect this article
Go from introduction to actual combat - context and task cancellation (notes)
io流代码
100 important knowledge points that SQL must master: creating calculation fields
Go from starting to Real - Interface (note)