I have table Salesorder and structure of the table is like this.
Create table Salesorder(SOID int identity, quantity numeric(18,2))
Values in the table is as mentioned below.
insert into Salesorder values(100)
insert into Salesorder values(Null)
insert into Salesorder values(200)
insert into Salesorder values(300)
insert into Salesorder values(Null)
insert into Salesorder values(Null)
insert into Salesorder values(500)
insert into Salesorder values(Null)
So, I want to update this table in that manner so that if I execute select query, I should find below result.
100
100
200
300
300
300
500
500
It means all null values should be updated with the previous not null values.
Here is working statement:
Update so1
Set quantity = (select top 1 quantity
from Salesorder so2
where so2.quantity is not null and
so2.SOID < so1.SOID
order by SOID desc)
From Salesorder so1
Where quantity is null;
Fiddle http://sqlfiddle.com/#!6/5a643/30
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With