Solved

Get the Value from a previous record using Oracle

Posted on 2012-03-27
5
546 Views
Last Modified: 2012-03-28
I have select statement where I need to get the date (LOG.Date) from the previous row. Please help?

Thanks

Select
 ,LOG.ID
 ,LOG.Date
 ,LOG.Name
From
 LOGS
Where LOG.Date >= trunc(sysdate)-45
ORDER BY LOG.Date
0
Comment
Question by:MIREESE
5 Comments
 
LVL 3

Expert Comment

by:zofcentr
Comment Utility
What does it mean previous record.

If previous means first record with LOG.Date<trunc(sysdate)-45
then use:

SELECT * FROM
FROM
(
Select
 ,LOG.ID
 ,LOG.Date
 ,LOG.Name
From
 LOGS
Where LOG.Date<trunc(sysdate)-45
ORDER BY LOG.Date DESC
) subQ
WHERE ROWNUM = 1;
0
 

Author Comment

by:MIREESE
Comment Utility
I'm sorry. The previous record would be either the first out of two or 99th out of 100. I need the value of the date column from that record to subtract to the value date column in the current record.

Thanks
0
 
LVL 6

Accepted Solution

by:
NikolasG earned 500 total points
Comment Utility
Hi,
You should the LEAD or LAG functions
http://www.oracle-base.com/articles/misc/LagLeadAnalyticFunctions.php

something like

SELECT empno,
       ename,
       job,
       sal,
       LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_prev,
       sal - LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_diff
FROM   emp;
Select
 ,LOG.ID
 ,LOG.Date
 LAG(LOG.Date, 1) OVER (ORDER BY LOG.Date) AS date_prev,
 ,LOG.Name
From
 LOGS
Where LOG.Date >= trunc(sysdate)-45
ORDER BY LOG.Date

Open in new window


Hope it helps
0
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
Comment Utility
expert nicholas has given the exact solution, besides this you might also need to consider the situation where previous row is null, ie.,incase the offset in lag function is not defined it may return null which may result in exception of when no_data_found in query so to avoid it just add the offset to the above query which nicholas has said, like this(considering sysdate-45 is default date)

Select
 ,LOG.ID
 ,LOG.Date
 LAG(LOG.Date, 1,trunc(sysdate-45)) OVER (ORDER BY LOG.Date) AS date_prev,
 ,LOG.Name
From
 LOGS
Where LOG.Date >= trunc(sysdate)-45
ORDER BY LOG.Date
0
 

Author Closing Comment

by:MIREESE
Comment Utility
The second answer was spot on!

Thanks so much for your help!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to recover a database from a user managed backup

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now