这个SQL的逻辑有点难理解,请高手指教:
select deptno, ename, sal
from emp e1
where
(select count(1)
from emp e2
where e2.deptno=e1.deptno and e2.ename!=e1.ename and e2.sal>e1.sal)
<2
order by deptno, sal desc;
是不是先执行
select count(1)
from emp e2 , emp e1
where e2.deptno=e1.deptno and e2.ename!=e1.ename and e2.sal>e1.sal
如果是这样,返回的值是26,怎么可能比数字2小?请高手指教下。
select deptno, ename, sal
from emp e1
where
(select count(1)
from emp e2
where e2.deptno=e1.deptno and e2.ename!=e1.ename and e2.sal>e1.sal)
<2
order by deptno, sal desc;
是不是先执行
select count(1)
from emp e2 , emp e1
where e2.deptno=e1.deptno and e2.ename!=e1.ename and e2.sal>e1.sal
如果是这样,返回的值是26,怎么可能比数字2小?请高手指教下。
即:每个部门下销售排前二的员工?
但是代码写不容易理解,修改一下会好些select deptno, ename, sal
from (select e.*,
row_number() over(partition by deptno order by sal desc) rn
from emp e)
where rn <= 2;
本来就是一个逻辑上不是很清晰的SQL,找到SQL编写的目的,重新编写替换之