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ÎÒÊÇÏëʵÏÖÕâÑùµÄ¹¦ÄÜ£¬µ«ÎÒÖªµÀÕâÑùд¿Ï¶¨