Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2011-03-22
Medium Priority
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 375 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

Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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