1. ntile
--ntile select e.employee_id, e.last_name, e.salary, e.department_id, ntile(10) over(order by e.salary desc) bucket --根据工资排序后,员工分配到10个组里,可以理解为工资相近的会被分配到相同的bucket from employees e;
2.percentile_cont,percentile_disc
--percentile_cont(P) 如果共有N行数据,及RN = (1 + P * (N - 1)),CRN = CEILING(RN), FRN = FLOOR(RN) /*If (CRN = FRN = RN) then the result is (value of expression from row at RN) Otherwise the result is (CRN - RN) * (value of expression for row at FRN) + (RN - FRN) * (value of expression for row at CRN)*/ --percontile_disc(0.5) 如果cume_dist计算的结果中有等于0.5的返回,如果没有取比0.5大的最小的一个值 --作为聚合函数 select e.department_id, percentile_cont(0.5) within group(order by e.salary), percentile_disc(0.5) within group(order by e.salary) from employees e group by e.department_id; select e.employee_id, e.last_name, e.salary, e.department_id, percentile_cont(0.5) within group(order by e.salary) over(partition by e.department_id), percent_rank() 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, percentile_disc(0.5) within group(order by e.salary) over(partition by e.department_id), cume_dist() over(partition by e.department_id order by e.salary) from employees e;
3. ratio_to_report
select e.employee_id, e.last_name, e.department_id, e.salary, ratio_to_report(e.salary) over(partition by e.department_id) --查询每个人的工资在部门内的占比 from employees e order by e.department_id;
原文:http://www.cnblogs.com/yasun/p/5178998.html