Skip to content
Advertisement

Multiply two existing columns and add result as new column in table

I’ve created a new column ‘jahresbeitrag’ and now I’m trying to multiply two numeric columns to save the values from the query result in this column, but for some reason the query doesn’t work. PgAdmin start executing the query, but it stays at …waiting for the query to complete. Don’t know what I’m doing wrong.

Result should be:

beitrag | wert | jahresbeitrag
54.3   *   2   =   108.6

My query

UPDATE test2 
SET jahresbeitrag = total.new
FROM (SELECT beitrag_netto * wert AS  new FROM test2) AS total;

Advertisement

Answer

I think you are looking for a generated/virtual column.

alter table test2 add column
jahresbeitrag float GENERATED ALWAYS AS (beitrag_netto * wert) STORED;

In this way you don’ t need to update your table, it is automatically calculated.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement