[SOLVED] Copy value to another row based on similar ID

Issue

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

Solution

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

Answered By – ahmet hamza emra

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.