It is not terribly important. But since I found the solution, I thought I’d share it with you anyway. During our Oracle 7Up workshop on SQL last week we discussed at length the use of Analytical Functions. Part of that discussion is of course the use of the Rank function in combination with Partitions and the Lead function.

One of the queries we specifically looked at was the one that tells us for each Employee how they rank within their own department in terms of their salary. And we threw in the difference for each employee’s salary and the next person on the list’s salary, for good measure.

That query looks something like this:

`select ename<br />,      deptno<br />,      sal<br />,      rank() over ( partition by deptno<br />                     order by sal desc<br />                   ) sal_rank_in_dept<br />,      sal - lead(sal, 1, null) over ( partition by deptno<br />                                       order by sal desc<br />                                     ) lead_over_next_value<br />from   emp<br />order<br />by     deptno<br />,      sal desc <br />`

The result of that query is:

Seems about right, does it not? Gives a lot of people a good feeling. They can see how they rank and what they earn more than the next person on the list. However, we have complaints from Mr. Scott. He says that this list is not correct, does not do him, justice. He is first on the list in Department 20, yet the list says he does not earn anything more than the next person on the list. Well, that is what it says. And it is the correct answer: SCOTT has the same salary as FORD. By coincidence, SCOTT is ranked higher than FORD. That means that the lead_over_next_value for SCOTT is calculated with regard to FORD’s salary. Since SCOTT and FORD have the same salary, the outcome for SCOTT is a zero result.

Well, to please SCOTT, we can try to change the query. Instead of calculating the difference with the next person on the list – as we do right now – we can try to calculate the difference with the next value on the list (the next value that differs from the current value). In SCOTT’s case that means we compare his salary to the 2975 that is the next differing value in Department 20.

To cut a long story short, the query that returns that result is this one:

` select ename<br /> ,      deptno<br /> ,      sal<br /> ,      rank() over ( partition by deptno<br />                      order by sal desc<br />                    ) sal_rank_in_dept<br /> ,      first_value(sal) over ( partition by deptno<br />                                order by sal desc<br />                                range between 1 following<br />                                      and     unbounded following<br />                          )  next_different_salary<br /> ,      sal - first_value(sal) over ( partition by deptno<br />                                      order by sal desc<br />                                      range between 1 following<br />                                            and     unbounded following<br />                          )  lead_over_next_value<br /> from   emp<br /> order<br /> by     deptno<br /> ,      sal desc <br />`

The query result:

Now SCOTT has what he wants: he leads the next person with a different salary by 25! Good for him.

We make use of a small yet neat trick: we create a sliding window defined using the RANGE clause. We want to have the value of the salary of the first person (first_value(sal) in our department (partition by deptno) that has a lower salary (order by sal desc) than the current record. By having the range start at 1 following, we ensure that we will not get the salary from someone with the same salary as the current employee.

Note: at first I tried to use LEAD( sal,1) with the same OVER() clause. However, in my 10.1.2 database this did not work: I got error messages:

`range between 1 following<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *<br />ERROR at line 9:<br />ORA-00907: missing right parenthesis<br />`

Share.