常用分析函数:
1. first,last
--假设a := min(奖金) keep(dense_rank first order by 工资)--假设工资最少为1000,a为在工资等于1000的员工取最小的奖金--假设b := min(奖金) keep(dense_rank last order by 工资)--假设工资最多为9999,b为在工资等于9999的员工取最小的奖金--作为聚合函数select e.department_id, min(e.hire_date) keep(dense_rank first order by e.salary), --工资最低的员工中,最早入职的入职日期 max(e.hire_date) keep(dense_rank first order by e.salary), --工资最低的员工中,最晚入职的入职日期 min(e.hire_date) keep(dense_rank last order by e.salary), --工资最高的员工中,最早入职的入职日期 max(e.hire_date) keep(dense_rank last order by e.salary) --工资最高的员工中,最晚入职的入职日期 from employees e group by e.department_id; --作为分析函数select e.last_name, e.salary, e.department_id, e.manager_id, min(e.hire_date) keep(dense_rank first order by e.salary) over(partition by e.department_id), --每个部门工资最低的员工中,最早入职的入职日期 max(e.hire_date) keep(dense_rank first order by e.salary) over(partition by e.department_id), --每个部门工资最低的员工中,最晚入职的入职日期 min(e.hire_date) keep(dense_rank last order by e.salary) over(partition by e.department_id), --每个部门工资最高的员工中,最早入职的入职日期 max(e.hire_date) keep(dense_rank last order by e.salary) over(partition by e.department_id) --每个部门工资最高的员工中,最晚入职的入职日期 from employees e;
2.first_value,last_value只能作为分析函数使用
--first_value,last_valueselect e.employee_id, e.last_name, e.salary, e.department_id, first_value(last_name) over(partition by e.department_id) --部门内第一个员工(排序方式好像不是稳定的,可以先排序再开窗) from employees e;select e.employee_id, e.last_name, e.salary, e.department_id, last_value(last_name) over(partition by e.department_id) --部门内最后一个员工(排序方式好像不是稳定的,可以先排序再开窗) from employees e;select e.employee_id, e.last_name, e.salary, e.department_id, first_value(last_name) over(partition by e.department_id order by e.salary) --部门内部按照价格升序阶梯取第一个人(工资最低的人的姓名),因为是价格升序所以部门内部第一个人的姓名不会变化 from employees e;select e.employee_id, e.last_name, e.salary, e.department_id, last_value(last_name) over(partition by e.department_id order by e.salary) --部门内部按照价格升序阶梯取最后一个人(工资最高的人的姓名),因为是价格升序所以部门最后一个人的的姓名有可能变化 from employees e;
--nth_value(column,n) from first/last over()--from first正序取,from last倒序取--取排序后的第n个column的值select e.employee_id, e.last_name, e.salary, e.department_id, nth_value(last_name, 3) from last over(partition by e.department_id) from employees e;
3.lag,lead只能作为分析函数使用
--lag(arg1,arg2,arg3)--查询当前行之前的第arg2行数据的arg1的值,如果未找到默认为arg3,如果arg3不声明,默认为空--lead(arg1,arg2,arg3)--查询当前行之后的第arg2行数据的arg1的值,如果未找到默认为arg3,如果arg3不声明,默认为空select e.last_name, e.hire_date, e.salary, lag(e.salary, 1, 0) over(order by e.hire_date)--自己之前入职的第一个员工的工资 from employees e; select e.last_name, e.hire_date, e.salary, lead(e.salary, 1, 0) over(order by e.hire_date)--自己之后入职的第一个员工的工资 from employees e;
4. list_agg
--作为单组分组函数select listagg(e.last_name, ';') within group(order by e.hire_date), --部门号为30的所有员工姓名 min(e.hire_date), max(e.hire_date) from employees e where e.department_id = 30;--作为聚合函数select listagg(e.last_name, ';') within group(order by e.hire_date), --各部门的所有员工姓名 min(e.hire_date), max(e.hire_date) from employees e group by e.department_id;--作为聚合函数select e.last_name, e.department_id, listagg(e.last_name, ';') within group(order by e.hire_date) over(partition by e.department_id) --over后不能加order by from employees e;
5.median 中位数