Can I set a formula for a specific column in SQL?

I want to implement something like Col3 = Col2 + Col1 in SQL.

This is somewhat similar to Excel, where each value in column 3 is the sum of the corresponding values ​​from column 2 and column 1.

+3
source share
3 answers

Yes, you can do this in SQL using the UPDATE command:

UPDATE TABLE table_name
SET col3=col1+col2
WHERE <SOME CONDITION>

This assumes that you already have a table filled with col1 and col2, and you want to populate col3.

+2
source

See Computed Columns

, . , , , .

CREATE TABLE J

-

CREATE TABLE dbo.mytable 
( low int, high int, myavg AS (low + high)/2 ) ;
+3

Yes. If it does not aggregate data row by row.

suppose that col1and col2are integers.

SELECT col1, col2, (col1 + col2) as col3 FROM mytable
+2
source

Source: https://habr.com/ru/post/1736266/


All Articles