有个表A
 r1    r2    r3
-----------------
0101   a     10
0101   a     20
0101   b     14
0101   c     23
0202   d     69
0202   f     35
0202   f     19
0202   z     88我想根据r1对r3进行汇总,但我也想把r2带出来(按道理应该r1相同的r2也相同,但是人为输入时可能会导致输入不一致,因为r2是辅助信息。)想得到这样的结果。 r1    r2    r3
-----------------
0101   a     67
0202   d     211大家帮帮忙,弄完这个好回家过年!

解决方案 »

  1.   


    create table t1(r1 varchar(10),r2 varchar(10),r3 int)insert into t1
    select '0101','a',10 union all
    select '0101','a',20 union all
    select '0101','b',14 union all
    select '0101','c',23 union all
    select '0202','d',69 union all
    select '0202','f',35 union all
    select '0202','f',19 union all
    select '0202','z',88select r1,min(r2),sum(r3) from t1 group by r1
      

  2.   

    select r1, min(r2), sum(r3), 
    from A 
    group by r1
      

  3.   

    select r1, min(r2) as r2, sum(r3) as r3, 
    from A 
    group by r1