Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update queery for max date after a new eposide

Status
Not open for further replies.

aspag

Programmer
Jan 15, 2004
17
US
I have a table that has the following layout or customer id, begindate and enddate.

1.updated field prevenddt
select a.cust,a.begindt,a.enddt
daysprev = 0
,(select max(enddt) from #t1 as c where( c.cust = a.cust and c.enddt < a.enddt)) as prevenddt
into #t2 from #t1as a

2.calculated daysprev by datediff(day,prevenddt,begindt)+1
3.If the customer days were either zero or more than 30 days, it was considered as a new eposide.
4. Updated NewBeginDt = begindt where neweposide = 1

Cust begindt enddt prevenddt daysprev NewEposide newbegindt
abc 1/16/2003 1/16/2003 1/16/2003 0 1 1/16/2003
abc 2/20/2003 2/20/2003 1/16/2003 36 1 2/20/2003
abc 3/11/2003 3/13/2003 2/20/2003 20 0 NULL
abc 3/25/2003 3/27/2003 3/13/2003 13 0 NULL
abc 4/15/2003 4/15/2003 3/27/2003 20 0 NULL
abc 4/17/2003 4/17/2003 4/15/2003 3 0 NULL
abc 4/22/2003 4/22/2003 4/17/2003 6 0 NULL
abc 4/24/2003 4/24/2003 4/22/2003 3 0 NULL
abc 5/13/2003 5/15/2003 4/24/2003 20 0 NULL
abc 5/22/2003 5/22/2003 5/15/2003 8 0 NULL
abc 5/27/2003 5/29/2003 5/22/2003 6 0 NULL
abc 8/5/2003 8/7/2003 5/29/2003 69 1 8/5/2003
abc 8/12/2003 8/14/2003 8/7/2003 6 0 NULL
abc 8/25/2003 8/25/2003 8/14/2003 12 0 NULL
abc 8/27/2003 8/27/2003 8/25/2003 3 0 NULL

Stuck with this query:
I still have to update a new additional column &quot;NewEndDt&quot;
which should be the max enddate of an eposide.
Is there a way to write a select statement that will return &quot;newenddt&quot; into into 1 record like:

Cust NewBegindt new enddt
abc 1/16/2003 1/16/2003
abc 2/20/2003 5/29/2003
abc 8/5/2003 8/27/2003

I hope this makes sense. Thanks for you help.


as
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top