1、用case when针对不同字段值做处理:
优化前:
select a.aa,x,y from
(select aa,sum(x) x from a where 字段 = 'A') a,
(select aa,sum(y) y from a where 字段 = 'B') b
where a.aa = b.aa
优化后:
select aa,
sum(nvl((case when 字段 = 'A' then x else null end),0)) x,
sum(nvl((case when 字段 = 'B' then y else null end),0)) y
from a
where 字段 in( 'A','B' )
group by aa