sql - oracle中的dense_rank()解析函数

标签 sql oracle oracle10g analytic-functions

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 byorder by 中引用了 salgroup 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/

相关文章:

sql - Oracle 22 Joins or over is not allowed in a select statement? ORA-01445

oracle - 立即执行且缺少无效选项

sql - Oracle,SQL,如何获取日期之间的间隔

sql - 如何从结束日期获取事件的开始日期和结束日期

java - 如何使用 Servlet 和 JSP 显示 PDF 文档?

sql - 当两者都可能更改时,按公共(public)用户 ID 或事务 ID 对所有行进行分组

mysql - 在我的数据库上创建表时出错

sql - 在自引用表上编写递归 SQL 查询

mysql - 计算表中元素的数量或获取最后一条记录哪个更快?

oracle10g - 在插入语句中使用两个 .nextval