Solved

Get the Value from a previous record using Oracle

Posted on 2012-03-27
5
550 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Expert Comment

by:zofcentr
ID: 37775114
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
ID: 37775159
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
ID: 37775202
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
ID: 37775831
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
ID: 37777160
The second answer was spot on!

Thanks so much for your help!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
make null the repeated levels 2 37
Oracle 12c Default Isolation Level 17 56
how to tune the query 17 59
Oracle perfomance issue. 4 23
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

726 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