Solved

How do I create date ranges based on a group of transactions?

Posted on 2008-06-25
7
255 Views
Last Modified: 2010-04-21
I have the following table:

PO          EFF_Date
1            01/01/2008
1            02/01/2008
1            03/01/2008
2            04/01/2008
2            05/01/2008

I want to build a table that looks like the following. The beg date will be the EFF_Date and the end date will be the day before the next EFF_Date. The last record of the PO will be "01/01/2010".

PO          Beg_Date             End_Date
1            01/01/2008           01/31/2008
1            02/01/2008           02/28/2008
1            03/01/2008           01/01/2010
2            04/01/2008           04/30/2008
2            05/01/2008           01/01/2010


I know there is a way of joining a table to itself to do this I just do not know how to do it.
0
Comment
Question by:Bob_Sheppard
  • 3
  • 2
  • 2
7 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 21869499
It is very expensive to join a table to itself this way, you are much more efficient doing this in your application logic...... but here goes

select po, Eff_Date as Beg_date, (select top 1 Eff_Date from table where eff_date>Beg_date order by eff_date) as End_Date
0
 

Author Comment

by:Bob_Sheppard
ID: 21869512
What about the last record within the PO.
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 250 total points
ID: 21869548
Oh, that won't work... maybe this:

select po, Eff_Date as Beg_date, coalesce((select top 1 Eff_Date - 1 from table where eff_date>Beg_date order by eff_date),'1/1/2010') as End_Date
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 74

Expert Comment

by:sdstuber
ID: 21869665
SELECT po, eff_date beg_date,
       NVL(LEAD(eff_date) OVER(PARTITION BY po ORDER BY eff_date ASC) - 1,
           TO_DATE('01/01/2010', 'mm/dd/yyyy')
          ) end_date
  FROM yourtable
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 21869791
oh, that's oracle syntax.

in version 2005, you should still be able to use the LEAD analytic function.

try this...


SELECT po, eff_date beg_date,
       coalesce(LEAD(eff_date) OVER(PARTITION BY po ORDER BY eff_date ASC) - 1,01/01/2010 ) end_date
  FROM yourtable
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 21869802
oh nevermind,  sorry,  I'm thinking of different analytics (like row_number)
0
 

Author Closing Comment

by:Bob_Sheppard
ID: 31470737
I just needed to add a match to the PO in addition to the EFF Date for this to be correct. The concept was right on though.

Thanks
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

821 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