mssql sqlserver 同时对一行中多列数据进行求和sum计算


摘要:
求和函数中,通常指使用sum对列进行聚合计算,下文讲述同时对多列多行采用sum计算,如下所示:
实验环境:sqlserver 2008 R2


  ---sum用法:
   select sum(表达式) from [表名]  where [条件]
  ---sum多列汇总求和
   select sum([列1]+[列2]+[列3]+...) from   [表名]  where [条件]  
  ---例:
  create table test(A int,B int,info varchar(10))
   go

   insert into test(A,B,info)values(5,6,'A'), 
    (2,4,'B'),(8,9,'C'),(3,1,'D'),(7,3,'E')
   go
  
   --例:对列A,B相加后再汇总 ,
   --对A汇总  ,对B汇总

    select sum(A+B) as [A+B],
     sum(A) as [A汇总],
     sum(B) as [B汇总]
       from test 
 
    go
    truncate table test
    drop     table test
mssql_sqlserver_多列汇总sum

mssql_sqlserver_多列汇总sum