Home > Back-end >  Copy value to another row based on similar ID
Copy value to another row based on similar ID

Time:03-12

I am looking for a way to achieve the following in SQL:

Say I have a table:

UniqeID AccountNo Value
abc123 001ID stack500
efg567 001ID null

What I am trying to achieve is if AccountNo are same, I would like to fill that null with the value above it. Basically, for my purposes, if AccountNo. then value must be same. I tried the approach described here

CodePudding user response:

Try the following:

UPDATE ttable --(or t1)
SET t1.Value = t2.Value 
FROM 
    ttable as t1
JOIN ttable as t2 ON t1.AccountNo = t2.AccountNo
WHERE t1.value IS NULL
AND t2.value IS NOT NULL
  • Related