oracle analytic functions

row_number() over(partition by ... order by ...)  

rank() over(partition by ... order by ...)  

dense_rank() over(partition by ... order by ...)  

count() over(partition by ... order by ...)  

max() over(partition by ... order by ...)  

min() over(partition by ... order by ...)  

sum() over(partition by ... order by ...)  

avg() over(partition by ... order by ...)  

first_value() over(partition by ... order by ...)  

last_value() over(partition by ... order by ...)  

lag() over(partition by ... order by ...)  

lead() over(partition by ... order by ...)  

 

The difference between rank() and dense_rank() is:  

rank() is a jump sort, when there are two second places, the next is the fourth place  

dense_rank() is a continuous sort, and when there are two second places, it still follows the third place  

 

example:

SELECT emp.full_name,

       emp.salary,

       emp.manager_id,

       row_number() over(PARTITION BY emp.manager_id ORDER BY emp.salary DESC) row_number_dept, --部门排行

       rownum row_number, -- row number

       round((rownum + 1) / 4) page_number, --one page every 4 rows

       ntile(2) over(ORDER BY emp.salary DESC) page_number_nt, -- averagely divided into two categories

       

       AVG(emp.salary) over(PARTITION BY emp.manager_id) avg_salary_department, --The average salary of the department

       SUM(emp.salary) over(PARTITION BY emp.manager_id) sum_salary_department, -- the total salary of the department

       COUNT(emp.salary) over(PARTITION BY emp.manager_id) count_emp_department, --all employees in the department

       dense_rank() over(PARTITION BY emp.manager_id ORDER BY emp.salary DESC) rank_salary_dept, -- the salary ranking of the department

       dense_rank() over(ORDER BY emp.salary DESC) rank_salary_company, -- the person's company-wide ranking

       

       MIN(emp.salary) over(PARTITION BY emp.manager_id) min_salary_dept, -- the minimum salary of the department       

       MIN(emp.salary) keep(dense_rank FIRST ORDER BY emp.salary) over(PARTITION BY emp.manager_id) min_salary_dept_first, -- the minimum salary of the department

       first_value(emp.salary) over(PARTITION BY emp.manager_id ORDER BY emp.salary) min_salary_dept_firstv, -- the minimum salary of the department     

       

       MAX(emp.salary) over(PARTITION BY emp.manager_id) max_salary_dept, -- the maximum salary of the department 

       MAX(emp.salary) keep(dense_rank LAST ORDER BY emp.salary) over(PARTITION BY emp.manager_id) max_salary_dept_last, --部门的最高薪水 

       last_value(emp.salary) over(PARTITION BY emp.manager_id ORDER BY emp.salary) max_salary_dept_lastv, -- the highest salary of the department

       

       lag(emp.full_name, 1, '00') over(ORDER BY emp.salary DESC) last_persion, -- the person whose salary is before him

       lead(emp.full_name, 1, '00') over(ORDER BY emp.salary DESC) next_persion -- the person whose salary is after him

  FROM fwk_tbx_employees emp

ORDER BY emp.salary DESC;

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989246&siteId=291194637
Recommended