Solved

Compare values from the most recent row to the one just before it

Posted on 2013-01-28
2
325 Views
Last Modified: 2013-01-29
Hi all,

A table has a timestamp t, and some numeric column, n

I need sql statement that would compare n from the row where t is max [max(t)] to:
 the value of n in the row where t is right before max(t)
-- OR --
 the value of n in the row where t is the second before max(t)

To clarify:
t                                           n
2013-01-28 19.00.00           23
2013-01-28 19.00.01           88
2013-01-28 19.00.02           2
2013-01-28 19.00.03           3


I need sql statement that would compare 3 to:
 2
-- OR --
 88

Any help is appreciated
0
Comment
Question by:darrgyas
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 38829332
You need to generate row numbers and then join to yourself

With a as ( select row_number() over (order by t desc) as row , n from hourtable)
Select t1.n, t2.n
From a t1 join a t2 on t1.rown=t2.rown+1
0
 

Author Closing Comment

by:darrgyas
ID: 38831063
thank you
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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