我想求数据库某个表中的一列的总和,用什么sql语句呢?

解决方案 »

  1.   

    select sum(某一列) from 某个表
      

  2.   

    select sum(colName) from tbName
      

  3.   

    select sum(isnull(columnname,0)) from tablename
      

  4.   

    Sum
    返回在某一集合上对数值表达式求得的和。语法
    Sum(«Set»[, «Numeric Expression»])示例
    如果 USA、CANADA 和 MEXICO 成员的值分别为 1000、2000 和 3000,则以下示例返回 6000:Sum({USA, CANADA, MEXICO}, Sales.VALUE)以下示例更加直观,并且同样有效:Sum({USA, CANADA, MEXICO}, Sales)
      

  5.   

    declare @t table (id varchar(6),val int)
    insert into @t select 'A',1
    insert into @t select 'A',1insert into @t select 'B',1
    insert into @t select 'A',1
    insert into @t select 'C',1
    insert into @t select 'C',1
    insert into @t select 'B',1
    insert into @t select 'B',1
    insert into @t select 'E',1
    insert into @t select 'F',1
    select id,sum(val) as 统计 from @t group by id--------------------------------------------------------
    id      统计
    A 3
    B 3
    C 2
    E 1
    F 1