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)

作者:terry,如若转载,请注明出处:https://www.web176.com/oceanbase/26267.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年10月14日
下一篇 2023年10月14日

相关推荐

发表回复

登录后才能评论