I’m using SQL to perform a basic calculation and I can’t figure out the correct syntax. I have a table where I want to add a new column, and to populate the values of that column using a combination of the existing values. Here is the code that I am illustrating the problem with.
-- create a table
CREATE TABLE test (
x numeric(10,3),
y numeric(10,3)
);
-- add some sample values
INSERT INTO test (x,y) VALUES( 7,3 );
INSERT INTO test (x,y) VALUES( 8,4 );
-- add a new column
ALTER TABLE test ADD testcalc numeric(10,3);
-- values in new column (testcalc) using the sum of values from x and y
INSERT INTO
test (testcalc)
SELECT
t.x + t.y
FROM
test as t;
This produces the following table:

I understand that the values are being inserted as new rows, but how can I add them as values to my column so that the table is structured like this?
x | y | testcalc
7 | 3 | 10
8 | 4 | 12
You need to use
UPDATEinstead ofINSERTin such a way in the last part of the query: