表 
姓名 合同(key) 期数(key) 付款
张xx 1001     1         200
张xx 1001     2         300
李xx 1002     1         500
李xx 1002     2         400我要的结果是按照相同合同合计付款总额,该怎么写
张xx 1001 500

解决方案 »

  1.   

    select 姓名,合同,sum(付款) as 付款总额 from tb group by 姓名,合同
      

  2.   

    create table tb(
    姓名 varchar(10),
    合同 varchar(10),
    期数 int,
    付款 int
    )insert into tb select '张xx',   '1001',           1,                   200 
    insert into tb select '张xx' ,  '1001' ,          2 ,                  300 
    insert into tb select '李xx'  , '1002'  ,         1  ,                 500 
    insert into tb select '李xx'   ,'1002'   ,        2   ,                400 select   姓名,合同,sum(付款)   as   付款总额   from   tb   group   by   姓名,合同drop table tb/*
    姓名         合同         付款总额        
    ---------- ---------- ----------- 
    张xx        1001       500
    李xx        1002       900(所影响的行数为 2 行)
    */
      

  3.   

    分组合计?select 合同 , 付款 = sum(付款) from tb group by 合同