开发者

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

开发者 https://www.devze.com 2022-12-22 06:14 出处:网络
I want to implement something like Col3 = Col2 + Col1 i开发者_C百科n SQL. This is somewhat similar to Excel, where every value in column 3 is sum of corresponding values from column 2 and column 1.Ha

I want to implement something like Col3 = Col2 + Col1 i开发者_C百科n SQL.

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


Have a look at Computed Columns

A computed column is computed from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators.

Also from CREATE TABLE point J

Something like

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


Yes, you can do it 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 with populated col1 and col2 and you want to populate col3.


Yes. Provided it is not aggregating data across rows.

assume that col1 and col2 are integers.

SELECT col1, col2, (col1 + col2) as col3 FROM mytable
0

精彩评论

暂无评论...
验证码 换一张
取 消