select
sum(case a when 0 then b else 0 end) as qty1,
sum(case a when 1 then b else 0 end) as qty2,
sum(b) - qty1 - qty2 as qty3  //qty3我是想实现这样的功能,但我知道这样写肯定不对,不知道怎么能实现?
from table1
group by...

解决方案 »

  1.   

    select
    sum(case a when 0 then b else 0 end) as qty1,
    sum(case a when 1 then b else 0 end) as qty2,
    sum(b) - sum(case a when 0 then b else 0 end)-sum(case a when 1 then b else 0 end) as qty3  
    from table1
    group by...
      

  2.   

    sum(b) - qty1 - qty2 as qty3
    等同于
    sum(case a when 0 then 0 when 1 then 0 else b end) as qty3
      

  3.   

    sum(b) - qty1 - qty2 as qty3 替换成sum(b)-sum(case when a=0 or a=1 then b else 0)
      

  4.   

    或者:select qty1,qty2,b-qty1-qty2
    from(
    select 
    sum(case a when 0 then b else 0 end) as qty1, 
    sum(case a when 1 then b else 0 end) as qty2, 
    sum(b) as b
    from table1 
    group by..) b
      

  5.   

    select 
    sum(case a when 0 then b else 0 end) as qty1, 
    sum(case a when 1 then b else 0 end) as qty2, 
    sum(b) - qty1 - qty2 as qty3  //qty3我是想实现这样的功能,但我知道这样写肯定不对,不知道怎么能实现? 
    from table1 
    group by...select 
    sum(case a when 0 then b else 0 end) as qty1, 
    sum(case a when 1 then b else 0 end) as qty2, 
    (sum(b) - sum(case a when 0 then b else 0 end) - sum(case a when 1 then b else 0 end)) as qty3  //qty3我是想实现这样的功能,但我知道这样写肯定不对,不知道怎么能实现? 
    from table1 
    group by...