SELECT empno, deptno
dense_rank() OVER (PARTITION BY deptno
ORDER BY sal NULLS LAST) SRLNO
FROM emp
WHERE deptno IN (10, 20)
group by empno, deptno --,sal
ORDER BY deptno, SRLNO;
此查询无效,因为 Sal
应该在 group by 子句中。谁能解释为什么会这样,有没有其他方法可以在不更改 group by 子句的情况下在同一选择中获得排名?你只想根据薪水来排序。
EDIT
Suppose there is another column name commision in emp table and i have to group by deptno and commision and partition by deptno only ,So what will be the suggested answer :
SELECT empno, deptno,sum(sal)
dense_rank() OVER (PARTITION BY deptno
ORDER BY sal NULLS LAST) SRLNO
FROM emp
WHERE deptno IN (10, 20)
group by deptno,commision --,sal
ORDER BY deptno, SRLNO;
now how can i group by depno and commision and only partition by deptno.
如何在不同的列上分组并根据不同列的分区查找排名
最佳答案
不要group by
任何东西——你的partition by
会帮你完成!
更具体地说,由于您在 partition by
和 order by
中引用了 sal
,group by
需要知道它来对结果进行分组。但是,在这种情况下,您不需要 group by
,因为您的 dense_rank
正在使用 partition by
子句中的自己的分区,并且不会遵循 group by
中的任何内容。
关于您的编辑,请在此处使用您的 over
子句:
sum(sal) over (partition by deptno, commission) 作为 salsum
关于sql - oracle中的dense_rank()解析函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8824813/