Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

FIRST_VALUE() and LAST_VALUE Analytic Functions

sathya_mounikaDec 8 2009 — edited Apr 23 2013
Hi All,

May be this is a basic question. But I am having a hard time understanding the difference between FIRST_VALUE() and LAST_VALUE() Analytical Functions.

As far as what I have read FIRST_VALUE function picks up the first record after the partition and order by and returns it after any computation. And the LAST_VALUE does the opposite. But the result of the second query what I expected did not show up (last value of the partition). It would be of some help if anyone could throw some light on it.
select empno
     , ename
     , sal
     , first_value(ename) over(order by sal desc)
  from emp;

empno  ename      sal       first_value 
------ ---------- --------- ----------- 
7839   KING       5000.00   KING        
7902   FORD       3000.00   KING        
7788   SCOTT      3000.00   KING        
7566   JONES      2975.00   KING        
7698   BLAKE      2850.00   KING        
7782   CLARK      2450.00   KING        
7499   ALLEN      1600.00   KING        
7844   TURNER     1500.00   KING        
7934   MILLER     1300.00   KING        
7654   MARTIN     1250.00   KING        
7521   WARD       1250.00   KING        
7876   ADAMS      1100.00   KING        
7900   JAMES      950.00    KING        
7369   SMITH      800.00    KING        

14 Row(s) affected
select empno
     , ename
     , sal
     , last_value(ename) over(order by sal desc)
  from emp;

empno  ename      sal       last_value 
------ ---------- --------- ---------- 
7839   KING       5000.00   KING       
7902   FORD       3000.00   SCOTT      
7788   SCOTT      3000.00   SCOTT      
7566   JONES      2975.00   JONES      
7698   BLAKE      2850.00   BLAKE      
7782   CLARK      2450.00   CLARK      
7499   ALLEN      1600.00   ALLEN      
7844   TURNER     1500.00   TURNER     
7934   MILLER     1300.00   MILLER     
7521   WARD       1250.00   MARTIN     
7654   MARTIN     1250.00   MARTIN     
7876   ADAMS      1100.00   ADAMS      
7900   JAMES      950.00    JAMES      
7369   SMITH      800.00    SMITH      

14 Row(s) affected
Thanks
Venkat
This post has been answered by Frank Kulash on Dec 8 2009
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2013
Added on Dec 8 2009
7 comments
1,965 views