Skip to content
Advertisement

One Table Update

I want to update Price where PriceGroup = ‘PG1’ and Price is null, with the Price from the same ID but the PriceGroup is PG2. I have tried a few solution here but couldn’t find any with Where statement.

PriceTable:

Fields: ID, Price, PriceGroup
ABC,null,PG1
ABC,1.00,PG2

Result will be:

ABC,1.00,PG1
ABC,1.00,PG2

Advertisement

Answer

Something like this?

Update priceTable
set price = (select b.price from PriceTable b where priceTable.id = b.id and price is not null )
where price is null

Tried with this data

INSERT INTO PriceTable VALUES(1, 1, 'PG1');
INSERT INTO PriceTable VALUES(1, null, 'PG2');
INSERT INTO PriceTable VALUES(2, 2,'PG3');
INSERT INTO PriceTable VALUES(2, null,'PG4');

and this is the output

before update
1|1|PG1
1||PG2
2|2|PG3
2||PG4
after update
1|1|PG1
1|1|PG2
2|2|PG3
2|2|PG4
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement