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...SQL code: 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...
sum(b) - qty1 - qty2 as qty3 等同于 sum(case a when 0 then 0 when 1 then 0 else b end) as qty3 sum(b) - qty1 - qty2 as qty3 替换成sum(b)-sum(case when a=0 or a=1 then b else 0) 或者:
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
就这个就可以了,看起来清爽,呵呵,谢谢各位 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我是想实现这样的功能,但我知道这样写肯定