我有一张表,有A,B,C两列字段! 
现在我希望计算出A的总和出B的总和然后判断!(sum(A)/sum(C))和(sum(B)/sum(C))。 
例如: 
select A as A ,B as B,(sum(A)/sum(B)) as C from 表名 
where having(sum(A)/sum(C))>2,having(sum(B)/sum(C))>2 
结果发现不能同时有两个having!!!!
请问应该怎么写?

解决方案 »

  1.   

    having 需要跟 group by 一起连用的你这个需求,可以使用下面的语句实现
    select a, b, c
      from (select a, b, sum(a)/sum(b) as c from tab
             group by a, b) t
     where a/b > 2
       and b/c > 2;
     
      

  2.   

    select sum(a),sum(b),sum(c) from t having sum(a)/sum(c) > 2 and sum(b)/sum(c) > 2;
      

  3.   

    你写的语法都有错误,having怎么可以放在where后面啊?
    只能有一个having,但你可以用and连接条件啊
      

  4.   

    若是或:select a,b,sum(a)/sum(b)
    from a
    having(sum(a)/sum(c))>2 
    group by a,b
    union 
    select a,b,sum(a)/sum(b)
    from a
    having(sum(b)/sum(c))>2 
    group by a,b;
    若是且:select t.a1,t.a2,t.a3 from (select a as a1,b as a2,sum(a)/sum(b) as a3,sum(a)/sum(c) as a4,sum(b)/sum(c) as a5
    from a
    group by a,b) t
    where t.a4>2 and t.a5>2;
    希望帮到你!
      

  5.   

    这是最简单的
    select a,b,sum(a)/sum(b) 
    from a
    having sum(a)/sum(c) > 2 and sum(b)/sum(c) > 2
    group by a,b;