暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle DENSE_RANK-聚合函数

askTom 2017-06-20
589

问题描述

下面是员工表,

EID名称工资通讯 _ pct
100史蒂文·24000
101 Neena 17000 0.6
102 Lex 17000 0.8
145约翰14000 0.4

如果执行以下 “选择到员工” 表,则输出为3。

在组中选择DENSE_RANK(17000,0.6) (按工资DESC,COMM_PCT DESC订购) “密集等级”
来自OE.员工;

所以我的假设是数据库在comm_pct列中计算0.6和0.8之间的体重年龄并决定排名...这是正确的吗?

专家解答

当您将inere group子句与dense_rank一起使用时,数据库:

-根据您的订单对行进行排序
-检查您传递给dense_rank的值 (v1,v2,...)
-确定您的值属于有序集中的位置
-根据dense_rank算法分配一个值 (领带获得相同的值,数字中没有间隙)

所以你得到的值三,因为如果你按sal desc排序行,comm desc你的数据看起来像:

EID  NAME   SALARY  COMM_PCT  
100  Steve  24000             
102  Lex    17000   0.8       
101  Neena  17000   0.6           <===== 17000, 0.6 fits here
145  John   14000   0.4 


17000,在第三位置0.6槽。之前没有任何联系。所以你得到了价值三。

这也适用于rank()。但是你会得到稍微不同的结果,因为在具有相同值的行之后,它从当前行号开始:

with rws as (
  select 100 eid, 'Steve' name, 24000 salary, null comm_pct from dual union all
  select 101 eid, 'Neena' name, 17000 salary, 0.6 comm_pct from dual union all
  select 102 eid, 'Lex' name, 17000 salary, 0.8 comm_pct from dual union all
  select 145 eid, 'John' name, 14000 salary, 0.4 comm_pct from dual
)
  select dense_rank(17000,0.6) within group (order by salary desc,comm_pct desc) dr_sal_comm,
         dense_rank(14000) within group (order by salary desc) dr_sal,
         rank(14000) within group (order by salary desc) rk_sal
  from   rws r;

DR_SAL_COMM  DR_SAL  RK_SAL  
3            3       4  
  
with rws as (
  select 100 eid, 'Steve' name, 24000 salary, null comm_pct from dual union all
  select 101 eid, 'Neena' name, 17000 salary, 0.6 comm_pct from dual union all
  select 102 eid, 'Lex' name, 17000 salary, 0.8 comm_pct from dual union all
  select 145 eid, 'John' name, 14000 salary, 0.4 comm_pct from dual
)
  select r.*,
         dense_rank() over (order by salary desc,comm_pct desc) dr_sal_comm,
         dense_rank() over (order by salary desc) dr_sal,
         rank() over (order by salary desc) rk_sal
  from   rws r;

EID  NAME   SALARY  COMM_PCT  DR_SAL_COMM  DR_SAL  RK_SAL  
100  Steve  24000             1            1       1       
102  Lex    17000   0.8       2            2       2       
101  Neena  17000   0.6       3            2       2       
145  John   14000   0.4       4            3       4     

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论