Solved

DB2 Query - Display results from next row along with current row

Posted on 2011-03-22
2
470 Views
Last Modified: 2012-06-21
Hi,

I have a table which looks like this:

POI      SEQUENCE       START DATE
APO1     2011-01        2011-01-01
APO1     2011-02        2011-01-15
APO1     2011-03        2011-02-01
APO1     2011-04        2011-02-15
APO1     2011-05        2011-03-01
APO1     2011-06        2011-03-15

I need to derive another table from this table which looks something like this:

POI      SEQUENCE       START DATE     (NEXT_START_DATE - 1 DAY)
APO1     2011-01        2011-01-01     2011-01-14
APO1     2011-02        2011-01-15     2011-01-31
APO1     2011-03        2011-02-01     2011-02-14
APO1     2011-04        2011-02-15     2011-02-28
APO1     2011-05        2011-03-01     2011-03-14
APO1     2011-06        2011-03-15     -

Any pointers on how I can achieve this? I tried various combinations using OUTER JOIN and EXCEPTION JOIN. I am staring at a wall at this moment!!

Regards
Ali.
0
Comment
Question by:bhagatali
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 125 total points
ID: 35193296
with a as (
  select poi, sequence, start_date, row_number() over(partition by poi order by start_date asc rown
 from your_table)

select t1.poi, t1.sequence, t1.start_date,  t2.start_date - 1 day
from a t1 left outer join a t2
    on t1.poi = t2.poi and t1.rown = t2.rown-1
0
 

Author Closing Comment

by:bhagatali
ID: 35330037
Thanks for the help.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now