Avatar of mikha
mikha
Flag for United States of America asked on

how to perform calculations in sql insert/update statement in postgresql?

example contrived for this example. I have a product table and I need to update the amount column table ( float4 datatype) based on the increment percentage column. 


CREATE TABLE public."Product" (
   id int4 NOT NULL DEFAULT,
   "incrementPercentage" float4 NULL,    "amount" float4 NULL, ... );

Open in new window

the incrementPercentage column might have a value or it might be null , so i want to know if the update statement with a case statement will work best. also, would like after I would like to limit the value to 2 decimal places. 

UPDATE Product
SET amount =  CASE WHEN incrementPercentage IS NOT NULL THEN ((incrementPercentage /100)*incrementPercentage  + amount ) ELSE amount END

Open in new window


SQLPostgreSQL

Avatar of undefined
Last Comment
slightwv (䄆 Netminder)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy