当前位置:网站首页>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)
边栏推荐
- Xiao Wang's interview training task
- At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
- SQL必需掌握的100个重要知识点:创建计算字段
- Modify large online games through CE modifier
- oss上传调用的是哪个方法
- Prospects for enterprise digitalization (38/100)
- 流程控制任务
- 跟我一起AQS SOS AQS
- 今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
- 系统自带的karsonzhang/fastadmin-addons报错
猜你喜欢

集合代码练习

Let Ma Huateng down! Web3.0, hopeless

AI 绘画极简教程

JVM memory structure when creating objects

MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)

SQL必需掌握的100个重要知识点:过滤数据

体验Navicat Premium 16,无限重置试用14天方法(附源码)

squid代理服务器

SQL server for circular usage

MySQL performance optimization index function, hidden, prefix, hash index usage (2)
随机推荐
空指针异常
SQL必需掌握的100个重要知识点:组合 WHERE 子句
Process control task
Go from introduction to practice - polymorphism (note)
SQL Server for循环用法
Go from entry to practice -- CSP concurrency mechanism (note)
CORBA 架构体系指南(通用对象请求代理体系架构)
Codeforces Round #717 (Div. 2)
GoLand permanently activated
TreeSet details
系统自带的karsonzhang/fastadmin-addons报错
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
100 important knowledge points that SQL must master: creating calculation fields
Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
Common methods of string class
Go从入门到实战——共享内存并发机制(笔记)
ICML2022 | 可扩展深度高斯马尔可夫随机场
Focus! Tips for installing fonts on domestic computers
Day8 ---- 云资讯项目介绍与创建
Go from introduction to actual combat - panic and recover (notes)