Oracle兼容-语法-KEEP FIRST/LAST


1. 语法

  1. aggregate_function KEEP(dense_rank FIRST/LAST ORDER BY order_clause)
  2. [ OVER( [ query_partion_clause ] ) ]
  3. aggregate_function:
  4. [MIN | MAX | SUM | AVG | COUNT | VARIANCE | VAR_POP | STDDEV | STD | STDDEV_POP]

其中,variancevar_pop 是同义词,stddevstdstddev_pop 三个也是同义词。

2. 定义和用法

KEEP FIRST/LAST二者都是聚合函数和分析函数,它们对一组行中的一组值进行操作,这些行相对于给定的排序规范排名为第一或最后。如果只有一行列为FIRST或LAST,则聚合仅对具有一个元素的集合进行操作。

3. 示例

  1. greatsql> CREATE TABLE t1 (
  2. id INT,
  3. dept_id INT,
  4. age INT,
  5. name varchar(64),
  6. salary INT,
  7. PRIMARY KEY(id)
  8. );
  9. greatsql> INSERT INTO t1 VALUES
  10. (1, 1, 22, 'emp1', 8000),
  11. (2, 1, 30, 'emp2', 7000),
  12. (3, 1, 27, 'emp3', 9000),
  13. (4, 1, 25, 'emp4', 6000),
  14. (5, 1, 22, 'emp5', 5000),
  15. (6, 1, 30, 'emp6', 12000),
  16. (7, 1, 30, 'emp7', 9000),
  17. (101, 2, 27, 'emp101', 8000),
  18. (102, 2, 24, 'emp102', 5000),
  19. (103, 2, 24, 'emp103', 6000),
  20. (104, 2, 26, 'emp104', 6000),
  21. (105, 2, 27, 'emp105', 6000),
  22. (106, 2, 25, 'emp106', 3000),
  23. (201, 3, 28, 'emp201', 8000),
  24. (301, 4, 26, 'emp301', 9000),
  25. (302, 4, 29, 'emp302', 7000);
  26. greatsql> SELECT id, dept_id, age, salary,
  27. SUM(salary) KEEP(DENSE_RANK FIRST ORDER BY age)
  28. OVER(PARTITION BY dept_id) aggrsalary FROM t1;
  29. +-----+---------+------+--------+------------+
  30. | id | dept_id | age | salary | aggrsalary |
  31. +-----+---------+------+--------+------------+
  32. | 1 | 1 | 22 | 8000 | 13000 |
  33. | 5 | 1 | 22 | 5000 | 13000 |
  34. | 4 | 1 | 25 | 6000 | 13000 |
  35. | 3 | 1 | 27 | 9000 | 13000 |
  36. | 2 | 1 | 30 | 7000 | 13000 |
  37. | 6 | 1 | 30 | 12000 | 13000 |
  38. | 7 | 1 | 30 | 9000 | 13000 |
  39. | 102 | 2 | 24 | 5000 | 11000 |
  40. | 103 | 2 | 24 | 6000 | 11000 |
  41. | 106 | 2 | 25 | 3000 | 11000 |
  42. | 104 | 2 | 26 | 6000 | 11000 |
  43. | 101 | 2 | 27 | 8000 | 11000 |
  44. | 105 | 2 | 27 | 6000 | 11000 |
  45. | 201 | 3 | 28 | 8000 | 8000 |
  46. | 301 | 4 | 26 | 9000 | 9000 |
  47. | 302 | 4 | 29 | 7000 | 9000 |
  48. +-----+---------+------+--------+------------+
  49. 16 rows in set (0.01 sec)
  50. greatsql> SELECT id, dept_id, age, salary,
  51. SUM(salary) KEEP(DENSE_RANK LAST ORDER BY age DESC)
  52. OVER(PARTITION BY dept_id) aggrsalary FROM t1;
  53. +-----+---------+------+--------+------------+
  54. | id | dept_id | age | salary | aggrsalary |
  55. +-----+---------+------+--------+------------+
  56. | 2 | 1 | 30 | 7000 | 13000 |
  57. | 6 | 1 | 30 | 12000 | 13000 |
  58. | 7 | 1 | 30 | 9000 | 13000 |
  59. | 3 | 1 | 27 | 9000 | 13000 |
  60. | 4 | 1 | 25 | 6000 | 13000 |
  61. | 1 | 1 | 22 | 8000 | 13000 |
  62. | 5 | 1 | 22 | 5000 | 13000 |
  63. | 101 | 2 | 27 | 8000 | 11000 |
  64. | 105 | 2 | 27 | 6000 | 11000 |
  65. | 104 | 2 | 26 | 6000 | 11000 |
  66. | 106 | 2 | 25 | 3000 | 11000 |
  67. | 102 | 2 | 24 | 5000 | 11000 |
  68. | 103 | 2 | 24 | 6000 | 11000 |
  69. | 201 | 3 | 28 | 8000 | 8000 |
  70. | 302 | 4 | 29 | 7000 | 9000 |
  71. | 301 | 4 | 26 | 9000 | 9000 |
  72. +-----+---------+------+--------+------------+
  73. 16 rows in set (0.00 sec)
  74. greatsql> SELECT dept_id, SUM(salary) KEEP(DENSE_RANK FIRST ORDER BY age) FROM t1 GROUP BY dept_id;
  75. +---------+-------------------------------------------------+
  76. | dept_id | SUM(salary) KEEP(DENSE_RANK FIRST ORDER BY age) |
  77. +---------+-------------------------------------------------+
  78. | 1 | 13000 |
  79. | 2 | 11000 |
  80. | 3 | 8000 |
  81. | 4 | 9000 |
  82. +---------+-------------------------------------------------+
  83. 4 rows in set (0.01 sec)

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx