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.
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 Ajoin yourtable B on A.DataId = B.DataId -1 and A.personid = B.personidThis 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. |
 |
|
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 aSET a.Field1 = b.Field1FROM mytable A join mytable B on A.ID = B.ID +1WHERE a.Field1 IS NULLThanks for your help. I wouldnt have got this otherwise.Cheers |
 |
|
|
|
|