Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: getting the nearest number from a column
On 11 avr, 09:04, hasta..._at_hotmail.com wrote:
> On 10 avr, 22:56, Mladen Gogala <mgogala.SPAM-ME...._at_verizon.net>
> > While the row_number() and rank() tricks will work for the MIN function, they
> > will not work for the max() function. To do that, you will have
> > to order by desc.
>
Hmmm... I may be wrong, but I have a feeling that the semantic you are after is actually provided by FIRST_VALUE and LAST_VALUE (ignoring the windowing issue).
Contrast :
SQL> with close as (select ename,empno,deptno,sal,
2 first_value(sal) over (order by abs(sal-1400) rows between unbounded preceding and unbounded following) as cls 3 from emp)
ENAME EMPNO DEPTNO SAL ---------- ---------- ---------- ---------- TURNER 7844 30 1500 with :
SQL> with close as (select ename,empno,deptno,sal,
2 min(sal) over (order by abs(sal-1400) rows between unbounded preceding and unbounded following) as cls 3 from emp)
ENAME EMPNO DEPTNO SAL ---------- ---------- ---------- ---------- SMITH 7369 20 800
![]() |
![]() |