Solved

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

Posted on 2013-01-28
2
323 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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