select a, b, c, d, sum(qty1),sum(qty2),sum(qty3)
from
(select a,b,c,d, qty1,0,0
from table1
where ......
union all
select a,b,c,d, 0,qty1,0
from table1
where ......
union all
select a,b,c,d, 0,0,qty3
from table1
where ...... )
group by a,b,c,d
都是同一个表啊,干嘛还要union all呢,
select a, b, c, d, sum(qty1),sum(qty2),sum(qty3)
from table1
group by a,b,c,d;
效率肯定比现在要好! |