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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
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…

734 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