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

Posted on 2011-03-22
Last Modified: 2012-06-21

I have a table which looks like this:

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:

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!!

Question by:bhagatali
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
LVL 37

Accepted Solution

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

Author Closing Comment

ID: 35330037
Thanks for the help.

Featured Post

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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