try this
 
select (select sum(f2) from table  where f5=0 and f1=a.f1)+(select sum(f2+f4) from table where f5=1 and f1=a.f1)-(select f3 where f1=a.f1) from table a group by f1

解决方案 »

  1.   

    select f1, 
          'aa' = case when f5 = 0 then sum(f2) 
                      when f5 = 1 then sum(f2 + f4 - f3)
                 end 
    from t
    group by f1
      

  2.   

    可以用union来实现
    select field1, sum(aa) from
    (
    select field1,sum(field2) aa from table where field5=0
    group by field1
    union all
    select field1,sum(field2)+sum(field4) aa from table where field5=1
    group by field1
    union all
    select field1,-field3 aa from table where field5=1
    )group by field1上列语句中你可以将每个select 的field1去掉
    总的group by field1去掉也可
      

  3.   

    谢谢各位帮忙可是
    我用DainelLee(Iinexperience)的语句写出来结果报错
    服务器: 消息 512,级别 16,状态 1,行 1
    子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
    用nielisheng(阿土) 的语句写出来报错
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 '12月欠费表.if_owe' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 '12月欠费表.if_owe' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    我用lyyrw(咋暖还寒)的语句写的时候报错
    服务器: 消息 156,级别 15,状态 1,行 7
    在关键字 'group' 附近有语法错误。