我想知道是否有一种方法可以在不使用内置函数的情况下实现SQL分析函数。有人可以帮我弄这个吗。谢谢。
SELECT *,
ROW_NUMBER() OVER( PARTITION BY dept_id ORDER BY salary DESC ) AS rownum,
DENSE_RANK() OVER( PARTITION BY dept_id ORDER BY salary DESC ) AS denserank,
RANK() OVER( PARTITION BY dept_id ORDER BY salary DESC ) AS rnk
FROM emp;
答案 0 :(得分:3)
以下是三个等效表达式:
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;
这假设存在emp_id
以使行对“row_number”唯一。
答案 1 :(得分:1)
您可以使用相关的子查询执行此操作。
select dept_id,salary,
(select count(*) from emp e1 where e1.dept_id=e.dept_id and e1.salary>=e.salary) as rnum
from emp e
当没有关系时,这很有效。
答案 2 :(得分:0)
这适用于所有情况
select DEPT_ID,SALARY,
(select count(*)+1 from emp r where r.SALARY>o.SALARY and r.dept_id=o.dept_id) **rank**,
(select count(distinct SALARY )+1 from emp r where r.SALARY>o.SALARY and r.dept_id=o.dept_id) *d_rank*,
(select count(*)+1 from (select x.*,rownum rn from ( select emp.* from emp order by DEPT_ID asc,salary desc ) x) r where r.rn<o.rn and r.dept_id=o.dept_id) **rownumm**
from (select x.*,rownum rn from ( select emp.* from emp order by DEPT_ID asc,salary desc ) x) o
order by DEPT_ID,salary desc;
排名:-使用((计数(小于当前行的值)+1)
对于密集排名:-与排名相同(计数不同值小于当前行)+1
row_number:-通过为每行生成rownum来创建嵌套查询,该行对于所有行都是不同的。现在最重要的是与等级逻辑相同 (大于上一个rownum(选择子查询的行数)的值的数量)+1