Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 update row with next highest value in same table

Author  Topic 

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2001-11-02 : 12:37:24
I have a need to update a date in a table with the date of the record that is just lower than it's date. Take the following table.

customer varchar(10),
account_date datetime,
group_id int,
account_date_prev datetime -- previous date

There are multiple occurences of customer, but the unique key is customer and account_date but I also need to group by group_id.

I wrote the following update but it is way to slow, there has to be a better way.


update cust
set account_date_prev = sl.mx

from (select P2.customer, P2.group_id, max(P2.account_date) as mx
from cust P
join cust P2 on P.customer = P2.customer and P.group_id = P2.group_id
and P.account_date > P2.account_date
group by P2.customer, P2.group_id) as sl


I've got to be missing something really basic but I just can't seem to see it.

Anyone?

Cat


edit: was trying to update the wrong column in the set.

Edited by - cat_jesus on 11/02/2001 13:08:52
   

- Advertisement -