Fseee
Fseee

Reputation: 2627

SQL SUM() function field

I have a database which contains some numerical fields; now i want to create another field which displays the sum of one of these fields. How can I create that field? thanks

Upvotes: 0

Views: 688

Answers (4)

David Aldridge
David Aldridge

Reputation: 52396

Why not define the derived value in a view and select from that?

Upvotes: 0

René Nyffenegger
René Nyffenegger

Reputation: 40613

SUM() leads to believe that you're talking about aggregate functions.

However, I think, in order for your question to make sense, you are really talking about summing a few values within one record. This assumption migth be wrong, if not, then you might try:

create view vw 
 as select 
   col_1, col_2, col_3, 
   col_1 + col_2 + col_3 as col_sum
from tbl

Upvotes: 0

David M
David M

Reputation: 72930

You could use a computed column:

ALTER TABLE table
ADD SumColumn AS Column 1 + Column2 + Column3

Upvotes: 2

Sheff
Sheff

Reputation: 3482

You could set up another field which is the result of of a function which calculates the sum sum() function, but this will have performance implications. A better solution maybe to set up a trigger which calculates the sum on insert or update of one of the other fields and then inserts it to the sum field

Upvotes: 0

Related Questions