Solved

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

Posted on 2011-03-22
2
477 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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 (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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

12 Experts available now in Live!

Get 1:1 Help Now