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!

Lag Function in Oracle

Kaushal SDec 9 2015 — edited Dec 9 2015

Hi All,

I would like to understand internal working of Lag function in Oracle, that might help in resolving my problem.

I have table like the below structure.

P_IDENTRY_TIMESTAMPSIZEC_ID
1528701-NOV-15 05.45.00 PM3092
1529501-NOV-15 06.00.00 PM1592
1530601-NOV-15 06.00.00 PM192

Now when i write a query like below:

select size, TO_TIMESTAMP(TO_CHAR(ENTRY_TIMESTAMP, 'YYYYMMDDHH24MI'), 'YYYYMMDDHH24MI') CURR_TIMESTAMP,

TO_TIMESTAMP(TO_CHAR(LAG(ENTRY_TIMESTAMP)  OVER(PARTITION BY C_ID ORDER BY ENTRY_TIMESTAMP ASC), 'YYYYMMDDHH24MI'),'YYYYMMDDHH24MI') LAG_TIMESTAMP

FROM Entry_log

where ENTRY_TIMESTAMP BETWEEN to_date('01-NOV-15 18.00.00','DD-MON-YY HH24.MI.SS') AND to_date('01-NOV-15 18.00.00','DD-MON-YY HH24.MI.SS')

Query will result in giving Lag_timestamp as 06.00.00 PM for size = 1 but will be NULL for size = 15.

Why is that Oracle never takes lag_timestamp = 06.00.00 PM for size = 15.

Could someone please explain this behaviour.

I am using Oracle 11g.

Thanks

This post has been answered by Kaushal S on Dec 9 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2016
Added on Dec 9 2015
4 comments
244 views