Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

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

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

like image 758
Jagira Avatar asked Mar 10 '10 08:03

Jagira


1 Answers

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 ) ;
like image 56
Adriaan Stander Avatar answered Oct 16 '22 07:10

Adriaan Stander