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
 Transact-SQL (2000)
 Cursor????

Author  Topic 

billsack
Starting Member

35 Posts

Posted - 2008-05-06 : 06:45:18
Hello experts.

I have some data that needs fixing. To keep it simple, it has an DataID field, PersonID field and a Date field.

The data is all sequential. When a record comes down that has the same PersonID as the previous record, it shows as NULL. I need to populate the rest of the fields in the data with the values from the previous row.

Does anyone have any ideas about how i could do this?

Thanks very much!

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2008-05-06 : 09:00:14
update yourtable
set yourtable.A.Datefield = coalesce(A.Datefield,B.Datefield)
from yourtable A
join yourtable B on A.DataId = B.DataId -1
and A.personid = B.personid


This assumes your DataId field is in sequential order. "Previous record" doesn't really mean anything without ordering.

An infinite universe is the ultimate cartesian product.
Go to Top of Page

billsack
Starting Member

35 Posts

Posted - 2008-05-06 : 10:37:26
Hi there, thanks very much.

I used your code but amended it slightly...

UPDATE a
SET a.Field1 = b.Field1
FROM mytable A
join mytable B
on A.ID = B.ID +1
WHERE a.Field1 IS NULL

Thanks for your help. I wouldnt have got this otherwise.

Cheers

Go to Top of Page
   

- Advertisement -