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

KEEP 函数必须与 MINMAXSUMAVGCOUNTVARIANCESTDDEV 函数一起使用。

语法

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

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

参数

参数

说明

OVER

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

expr

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

返回类型

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

示例

分析函数示例

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

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

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

  1. SELECT last_name, department_id, salary,
  2. SUM(salary) KEEP (DENSE_RANK FIRST ORDER BY salary)
  3. OVER (PARTITION BY department_id) "Worst",
  4. SUM(salary) KEEP (DENSE_RANK LAST ORDER BY HIREDATE)
  5. OVER (PARTITION BY department_id) "Best"
  6. FROM employees
  7. ORDER BY department_id, salary, last_name;

查询结果如下:

  1. +-----------+---------------+--------+-------+-------+
  2. | LAST_NAME | DEPARTMENT_ID | SALARY | Worst | Best |
  3. +-----------+---------------+--------+-------+-------+
  4. | Raphaely | 30 | 1700 | 1700 | 11000 |
  5. | De Haan | 30 | 11000 | 1700 | 11000 |
  6. | Errazuriz | 40 | 1400 | 1400 | 1400 |
  7. | Raphaely | 50 | 1700 | 1700 | 27500 |
  8. | Weiss | 50 | 13500 | 1700 | 27500 |
  9. | Hartstein | 50 | 14000 | 1700 | 27500 |
  10. | Russell | 90 | 13000 | 13000 | 13000 |
  11. | Partners | 90 | 14000 | 13000 | 13000 |
  12. +-----------+---------------+--------+-------+-------+
  13. 8 rows in set (0.01 sec)

聚合函数示例

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

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

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

  1. SELECT
  2. SUM(salary) KEEP (DENSE_RANK FIRST ORDER BY salary) "Worst",
  3. SUM(salary) KEEP (DENSE_RANK LAST ORDER BY HIREDATE) "Best"
  4. FROM employees;

查询结果如下:

  1. +-------+-------+
  2. | Worst | Best |
  3. +-------+-------+
  4. | 1400 | 27500 |
  5. +-------+-------+
  6. 1 row in set (0.00 sec)