Here are the three equivalent expressions:
select emp.*,
(select count(*)
from emp emp2
where emp2.dept_id = emp.dept_id and
(emp2.salary > emp.salary or
emp2.salary = emp.salary and emp2.emp_id <= emp.emp_id
)
) as "row_number",
(select 1 + count(*)
from emp emp2
where emp2.dept_id = emp.dept_id and
emp2.salary > emp.salary
)
) as "rank",
(select count(distinct salary)
from emp emp2
where emp2.dept_id = emp.dept_id and
emp2.salary >= emp.salary
) as "dense_rank",
from emp;
This assumes the existence of an emp_id
to make the rows unique for "row_number".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…