Reputation: 11
Using Oracle 12C Its Regarding Dense_Rank Within Group Aggregate function, this function takes two columns(salary,commission) existing in (emp)table and provide its rank. The results are little confusing. Rank is showed even though the values are not in database. There are two records with 3000.
Existing values in db for
sal, comm
3000,NULL
3000,50 (updated one of the comm value from NULL to 50 in EMP table)
Non Existing values in db for
sal, comm
3000,0
3000,100
3000,500
Is it giving possible ranks,if so then its giving same rank(3) for comm 100,500 and NULL. And equal rank(2) for comm 0 and 50.
ORDER BY SAL DESC AND 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;
Order by sal desc and comm asc
Here 49 is given x rank
50 is given y rank
51 and NULL are given z rank.
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;
Order by sal desc and comm desc
Here 49 is given x rank
50 and 51 is given y rank
NULL is given z rank.
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;
Upvotes: 1
Views: 1333
Reputation: 31648
It must not be concluded that the expressions assign the same rank because each of your DENSE_RANK
should be considered in isolation. So, 0 and 50 have same rank because when compared independently with the entries in the table, they both fall under the same rank.
DENSE_NULL DENSE_ZERO DENSE_50 DENSE_100 DENSE_500
---------- ---------- ---------- ---------- ----------
63 63 63 63 63
In case, you want to consider multiple hypothetical values to determine rank of the consolidated values, do a UNION ALL
with existing entries of the main table.
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
);
Which gives
DENSE_NULL DENSE_ZERO DENSE_50 DENSE_100 DENSE_500
---------- ---------- ---------- ---------- ----------
66 63 64 65 65
Upvotes: 0