Home > Software engineering >  Add calculated column to table ssms (sql query)
Add calculated column to table ssms (sql query)

Time:06-20

Im trying to add and save a new calculated column to the table energy, using the following sql query:

ALTER TABLE energy ADD energie_active_1 float 
SELECT *, energie_active - LAG(energie_active, 1, energie_active) OVER (PARTITION BY DESIGNATION ORDER BY TS) FROM energy

But i get the column energie_active_1 empty and a new column without name with the calculated values (as shwon in the image) Query result

I dont use sql queries a lot, so any help or advice would be very appreciated.

Thank you.

CodePudding user response:

alter table TABLE_NAME
add [column_name] as (**COLUMN-SQL**)

CodePudding user response:

Thank you all for your answers. So this is what i have came with, not sure if its the correct way to do it. But its working for me :)

INSERT INTO energy ( energy.TS
     , energy.DESIGNATION 
     , energy.energie_active,
     energy.energie_reactive,
     energie_active_1 ) (SELECT energy.TS
     , energy.DESIGNATION 
     , energy.energie_active,
     energy.energie_reactive,energy.energie_active-LAG(energy.energie_active, 1, energy.energie_active) OVER (PARTITION BY energy.DESIGNATION ORDER BY energy.TS)FROM energy)

Now i get the new column in the same table.

  • Related