簡體   English   中英

組內的匯總函數Dense_rank

[英]Aggregate Function Dense_rank within group

使用Oracle 12C的 Group Aggregate函數中有關Dense_Rank的功能,該函數采用(emp)表中存在的兩列(工資,佣金)並提供其排名。 結果有點混亂。 即使值不在數據庫中,也會顯示排名。 有兩個記錄為3000。

db中的現有值

薩爾,通訊

3000,空

3000,50(將EMP表中的comm值之一從NULL更新為50)

db中不存在的值

薩爾,通訊

3000,0

3000,100

3000,500

它是否給出可能的等級,如果是,那么它給comm 100,500和NULL給出相同的rank(3)。 通訊0和50等於等級(2)。

通過SAL DESC和COMM ASC訂購。

 select DENSE_rank (3000,null) within group(order by sal desc,comm ) DENSE_NULL,
        DENSE_rank (3000,0) within group(order by sal desc,comm) DENSE_ZERO,   
        DENSE_rank (3000,50) within group(order by sal desc,comm) DENSE_50,
        DENSE_rank (3000,100) within group(order by sal desc,comm) DENSE_100,
        DENSE_rank (3000,500) within group(order by sal desc,comm) DENSE_500
   from emp;

由Sal desc和comm asc排序

這里給定49 x等級

y等級為50

51和NULL被賦予z等級。


SELECT DENSE_RANK (3000,49)   WITHIN GROUP(ORDER BY sal DESC,comm) DENSE_49,   
       DENSE_RANK (3000,50)   WITHIN GROUP(ORDER BY sal DESC,comm) DENSE_50,
       DENSE_RANK (3000,51)   WITHIN GROUP(ORDER BY sal DESC,comm) DENSE_51,
       DENSE_RANK (3000,null) WITHIN GROUP(ORDER BY sal DESC,comm) DENSE_NULL
  FROM emp;

由sal desc和comm desc排序

這里給定49 x等級

50和51被賦予y等級

NULL賦予z等級。

SELECT DENSE_RANK (3000,49)   WITHIN GROUP(ORDER BY sal DESC,comm desc) DENSE_49,   
       DENSE_RANK (3000,50)   WITHIN GROUP(ORDER BY sal DESC,comm desc) DENSE_50,
       DENSE_RANK (3000,51)   WITHIN GROUP(ORDER BY sal DESC,comm desc) DENSE_51,
       DENSE_RANK (3000,null) WITHIN GROUP(ORDER BY sal DESC,comm desc) DENSE_NULL
  FROM emp;

不能斷定這些表達式分配相同的等級,因為應該單獨考慮每個DENSE_RANK 因此,0和50具有相同的等級,因為當表中的條目獨立比較時,它們都屬於同一等級

DENSE_NULL DENSE_ZERO   DENSE_50  DENSE_100  DENSE_500
---------- ---------- ---------- ---------- ----------
        63         63         63         63         63

如果要考慮多個假設值以確定合並值的等級,請對主表的現有條目執行UNION ALL

with hypoth(sal,comm) AS
(
  select 330,null from dual union all
  select 3000,0   from dual union all
  select 3000,50  from dual union all
  select 3000,500 from dual
)
 select DENSE_rank (3000,null) within group(order by sal desc,comm ) DENSE_NULL,
        DENSE_rank (3000,0) within group(order by sal desc,comm) DENSE_ZERO,   
        DENSE_rank (3000,50) within group(order by sal desc,comm) DENSE_50,
        DENSE_rank (3000,100) within group(order by sal desc,comm) DENSE_100,
        DENSE_rank (3000,500) within group(order by sal desc,comm) DENSE_500
   from ( select  salary as sal,commission_pct as comm from  employees 
          UNION ALL
          select sal,comm  from  hypoth
          );

這使

DENSE_NULL DENSE_ZERO   DENSE_50  DENSE_100  DENSE_500
---------- ---------- ---------- ---------- ----------
        66         63         64         65         65

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM