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 vs Last_Value Unexpected bevaviour

807684Aug 19 2011 — edited Aug 19 2011
Hi,

I am using analytical function first_value() to get the record with latest non null "status_date".

select id, first_value(status_date) over (partition by id order by status_date desc nulls last) from table1;

The above query works as I expected, as the default window range of <rows Between Unbounded preceding and unbounded following> gets applied with out me specifying it in the query.

But When I use last_value instead of first_value and do as below..

select id, last_value(status_date) over (partition by id order by status_date nulls first) from table1;

The above query do not work as expected, It looks like it doesn't use the the window range of <rows Between Unbounded preceding and unbounded following> by default., It looks like it applies the window range "current row" by default.

Inorder to work as expected I had to mention it in the query as below

select id, last_value(status_date) over (partition by id order by status_date nulls first rows Between Unbounded preceding and unbounded following) from table1;


Can someone please let me know why is this difference, or may be I am understanding the things wrong..

Thanks,
Vinay
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 16 2011
Added on Aug 19 2011
5 comments
1,045 views