more sql

From: Stephens, Chris <>
Date: Wed, 12 Mar 2008 12:09:54 -0500
Message-ID: <>

I have learned a lot about sql this morning but I can't quite close the gap between what I am reading and what I need to do. I know the following query can be written far more efficiently:  

select pstart.periodID, ps1.projectNumber, ps1.statusID, ps1.changeDate

from vw_project_status_period ps1,

           period pstart,

          period pstop

where pstart.periodid >= ps1.periodid and

              pstop.endDate = ( select max( p.endDate )

vw_project_status_period ps2,  

period p

ps2.projectnumber = ps1.projectNumber and  

ps2.periodid = p.periodid and p.endDate <= pstart.endDate

  ) and

             pstop.periodid = ps1.periodid;    

vw_project_status_period contains info on when a projects status changes. The above query fills in period gaps between changes for each project. I am having trouble figuring out how to 'fill in the gaps' of periods between the change dates. After that it is just a matter of using last_value.  

I have been looking at this for a few hours and figured I'd throw another sql question to the list.    

        This message is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this message is not the intended recipient or the employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by email reply.

Received on Wed Mar 12 2008 - 12:09:54 CDT

Original text of this message