窗口函数 first_value

cume_dist 如果按升序排列,则统计:小于等于当前值 的行数/总行数(number of rows ≤ current row)/(total number of rows)。如果是降序排列,则统计:大于等于当前值的行数/总行数。 示例: 1. 统计小于等于当前工资的人数占总人数的比例。 SELECT

 cume_dist

如果按升序排列,则统计:小于等于当前值的行数/总行数(number of rows ≤ current row)/(total number of rows)。如果是降序排列,则统计:大于等于当前值的行数/总行数。

示例:

1. 统计小于等于当前工资的人数占总人数的比例。

SELECT name, 
       dept_no, 
       salary,
       cume_dist() OVER (ORDER BY salary) as cume_dist
FROM data;


结果:


+-------+-------+------+---------+
|name   |dept_no|salary|cume_dist|
+-------+-------+------+---------+
|rose   |2      |4000  |0.125    |
|jack   |2      |5000  |0.375    |
|steven |3      |5000  |0.375    |
|john   |1      |6000  |0.5      |
|jerry  |2      |6600  |0.625    |
|tom    |1      |8000  |0.75     |
|richard|3      |9000  |0.875    |
|mike   |1      |10000 |1.0      |
+-------+-------+------+---------+

如果要统计大于等于当前工资的人数占总人数的比例,只需更改salary排序规则为降序desc即可。

2. 根据部门统计小于等于当前工资的人数占部门总人数的比例

SELECT 
    name, 
    dept_no, 
    salary,
    cume_dist() OVER (PARTITION BY dept_no ORDER BY salary) as cume_dist
FROM data;
知秋君
上一篇 2024-07-19 08:02
下一篇 2024-07-19 07:36

相关推荐