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
Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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
RPG to c# 3 376
AS400 Forms 9 134
finding tables 2 40
Shell Script on AIX 7 111
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

809 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