select distinct a.id,sum(a.fz) , (select sum(b.fz) from table_a as b
where b.fz < 0 and a.id = b.id) from table_a as a where a.fz>0

解决方案 »

  1.   

    select theID,sum(fz) from  table_a group by theID
      

  2.   

    select id,a.sum(fz) as '加分',b.sum(fz) as '减分'from 
    (select sum(fz),id from table_a where fz>0) a,
    (select sum(fz),id from table_a where fz<0) b  where a.id=b.id
      

  3.   

    在你的两句中间加入UNION,然后在最后加入ORDER BY id试试:)
      

  4.   

    非常感谢 welyngj(平平淡淡) ,你的方法我测试过是可行的。
    但是引发另外一个问题,你那种方法选择出来的是id既有正分又有副分的,如果id分数为全正或者全副则选择不出来。我改进一下可以解决这个问题:
    select id, a.JF,b.KF from 
                (select sum(fz) as JF,id from table_a where fz>0 group by id) as a
           left join
                (select sum(fz) as KF,id from table_a where fz<0 group by id) as b
    unionselect id, a.JF,b.KF from 
                (select sum(fz) as JF,id from table_a where fz>0 group by id) as a
           right join
                (select sum(fz) as KF,id from table_a where fz<0 group by id) as b
      

  5.   

    用下面的。
    select id,a.sum(fz) as '加分',b.sum(fz) as '减分'from 
    (select sum(fz),id from table_a where fz>0) a 
    full outer join
    (select sum(fz),id from table_a where fz<0) b  on a.id=b.id
      

  6.   

    能不能select sum(fz*iif(fz>0,1,0)) as JF,sum(fz*iif(fz>0,0,1)) as kF,id from table_a 
      

  7.   

    select theid
      ,JF=sum(case when FZ>0 then FZ else 0 end)
      ,KF=sum(case when FZ<0 then FZ else 0 end)
    from table_a group by theid