Solved

Get the Value from a previous record using Oracle

Posted on 2012-03-27
5
549 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Require data to appear on a single line 2 69
Oracle - SQL Query with Function 3 53
PL/SQL Two changes 7 34
Trying to get a Linked Server to Oracle DB working 21 69
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

856 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