Solved

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

Posted on 2008-06-25
7
226 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 73

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 73

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 73

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

744 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

10 Experts available now in Live!

Get 1:1 Help Now