开发者

SQL SUM() function field

I have a database which contains some numerical fields; now i want to create another field which displays t开发者_Go百科he sum of one of these fields. How can I create that field? thanks


You could use a computed column:

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


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


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


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜