标签归档:sum

mssql sqlserver sum求和函数简介(用法说明)


摘要:
下文讲述mssql sqlserver中sum函数用法说明,如下所示:
实验环境:sql server 2008 R2



sum函数语法简介
SELECT SUM(表达式)
FROM 表名
WHERE [筛选条件];
—参数说明——-
表达式:可以为一个字段也可以为一个特定的数值

例1:

     ----获取9月薪水大于13000的员工的总薪水支出
    select sum(xinShui)  as [总薪水支出]
     from [支出表]
     where [薪水] > 13000
               and  [支出月份] =9
  

例2:
sum同distinct一起使用,统计单一类型的合计数

     select sum(distinct xinShui)  as [总薪水支出]
        from [支出表]
          where [薪水] > 13000
              and  [支出月份] =9
  

例3:
sum统计两列之间的计算后求和

 
     select sum(A-B) from 
      tableName 
     where []
   

例4:
sum统计“单列计算后”求和

    select sum(A*0.88) from 
      tableName 
     where []
   

例5:
sum同group by 一起使用

    select userName,sum(xinShui) as [薪水] 
     from tableName 
     where  [支出月份] =9 
     group by userName 
   

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