codecamp

OceanBase KEEP

KEEP 函数用于对一组行中的值进行操作,将这组行按照给定的排序规则排序后返回排在第一或最后的值。作为聚合函数,KEEP 对所有行进行操作并返回单个输出行。作为分析函数,KEEP 基于 query_partition_clause 中的一个或多个表达式将查询结果集分为几组。

KEEP 函数必须与 MINMAXSUMAVGCOUNTVARIANCE 或 STDDEV 函数一起使用。

语法

KEEP (DENSE_RANK {FIRST | LAST} ORDER BY expr [ DESC | ASC ] [ NULLS { FIRST | LAST } ] [, expr [ DESC | ASC ] [ NULLS { FIRST | LAST } ]]... )
   [ OVER ( [query_partition_clause] ) ]

作为分析函数使用时,您需要使用窗口函数的完整语法,它对一组行的集合进行计算并返回多个值。作为聚合函数使用时,该函数对一组行的集合进行聚合计算,结果只能返回一个值,此时不需要加 OVER 关键字。

参数

参数

说明

OVER

使用 OVER 子句定义窗口进行计算。

expr

可以是任何表达式。度量列中的空值将被忽略。

返回类型

返回与度量列相同的数据类型。

示例

分析函数示例

建表 employees,并向里面插入数据,执行以下语句:

CREATE TABLE employees (department_id INT,manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
INSERT INTO employees VALUES(30, 100, 'De Haan', '2018-05-01',11000);      
INSERT INTO employees VALUES(40, 100, 'Errazuriz', '2017-07-21', 1400);
INSERT INTO employees VALUES(50, 100, 'Hartstein', '2019-10-05',14000);     
INSERT INTO employees VALUES(50, 100, 'Raphaely', '2017-07-22', 1700);
INSERT INTO employees VALUES(50, 100, 'Weiss',  '2019-10-05',13500);     
INSERT INTO employees VALUES(90, 100, 'Russell', '2019-07-11', 13000);
INSERT INTO employees VALUES(90,100, 'Partners',  '2018-12-01',14000);

对于每个部门:按照薪水排序,对排在第一的员工的薪水求和;按照雇用日期排序,对排在最后的员工的薪水求和。执行以下语句:

SELECT last_name, department_id, salary,
       SUM(salary) KEEP (DENSE_RANK FIRST ORDER BY salary)
         OVER (PARTITION BY department_id) "Worst",
       SUM(salary) KEEP (DENSE_RANK LAST ORDER BY HIREDATE)
         OVER (PARTITION BY department_id) "Best"
   FROM employees
   ORDER BY department_id, salary, last_name;

查询结果如下:

+-----------+---------------+--------+-------+-------+
| LAST_NAME | DEPARTMENT_ID | SALARY | Worst | Best  |
+-----------+---------------+--------+-------+-------+
| Raphaely  |            30 |   1700 |  1700 | 11000 |
| De Haan   |            30 |  11000 |  1700 | 11000 |
| Errazuriz |            40 |   1400 |  1400 |  1400 |
| Raphaely  |            50 |   1700 |  1700 | 27500 |
| Weiss     |            50 |  13500 |  1700 | 27500 |
| Hartstein |            50 |  14000 |  1700 | 27500 |
| Russell   |            90 |  13000 | 13000 | 13000 |
| Partners  |            90 |  14000 | 13000 | 13000 |
+-----------+---------------+--------+-------+-------+
8 rows in set (0.01 sec)

聚合函数示例

建表 employees,并向里面插入数据,执行以下语句:

CREATE TABLE employees (department_id INT,manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
INSERT INTO employees VALUES(30, 100, 'De Haan', '2018-05-01',11000);      
INSERT INTO employees VALUES(40, 100, 'Errazuriz', '2017-07-21', 1400);
INSERT INTO employees VALUES(50, 100, 'Hartstein', '2019-10-05',14000);     
INSERT INTO employees VALUES(50, 100, 'Raphaely', '2017-07-22', 1700);
INSERT INTO employees VALUES(50, 100, 'Weiss',  '2019-10-05',13500);     
INSERT INTO employees VALUES(90, 100, 'Russell', '2019-07-11', 13000);
INSERT INTO employees VALUES(90,100, 'Partners',  '2018-12-01',14000);

按照薪水排序,对排在第一的员工的薪水求和;按照雇用日期排序,对排在最后的员工的薪水求和。执行以下语句:

SELECT 
       SUM(salary) KEEP (DENSE_RANK FIRST ORDER BY salary) "Worst",
       SUM(salary) KEEP (DENSE_RANK LAST ORDER BY HIREDATE) "Best"
  FROM employees;

查询结果如下:

+-------+-------+
| Worst | Best  |
+-------+-------+
|  1400 | 27500 |
+-------+-------+
1 row in set (0.00 sec)
OceanBase COUNT
OceanBase LISTAGG
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

OceanBase 控制台指南

OceanBase ODC 使用指南

OceanBase Web 版 ODC

OceanBase 客户端版 ODC

OceanBase Connector/J 开发者指南

OceanBase 什么是OceanBase Connector/J

OceanBase SQL 参考(MySQL 模式)

OceanBase SQL 参考(Oracle 模式)

OceanBase 基本元素

OceanBase 数据库对象

OceanBase 函数

OceanBase 单行函数

OceanBase 返回数字的字符串函数

OceanBase 通用比较函数

OceanBase 编码解码函数

OceanBase SQL 调优指南

OceanBase 相关协议

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }