Home » SQL & PL/SQL » SQL & PL/SQL » SQL Look Back
SQL Look Back [message #199680] Wed, 25 October 2006 15:54 Go to next message
Duane
Messages: 452
Registered: December 2002
Senior Member
I read this somewhere in the Oracle documentation but I can't find it now. I'm talking about when you write an SQL statement and can look back at the previous record.

Table

column 1   column 2   column 3
7/15/2006    51          0
8/15/2006    51          1
9/15/2006    51          3
10/15/2006   51          0


I query for the latest date in the table but I also want to look back at the n-1 record. I need to grab the value in Column 3 for the n-1 record.

Anyone have an example of that?
Re: SQL Look Back [message #199681 is a reply to message #199680] Wed, 25 October 2006 16:16 Go to previous messageGo to next message
Littlefoot
Messages: 20891
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Use LAG function; something like this:
SELECT col1, LAG(col3) OVER (ORDER BY col1)
FROM your_table;
Re: SQL Look Back [message #199683 is a reply to message #199681] Wed, 25 October 2006 16:58 Go to previous message
Duane
Messages: 452
Registered: December 2002
Senior Member
Perfect! Thanks.
Previous Topic: How to update a table with millions of rows
Next Topic: to get highest sal dept-wise
Goto Forum:
  


Current Time: Sat Dec 03 21:56:36 CST 2016

Total time taken to generate the page: 0.07628 seconds