sleect a.部门,
sum(case when b.正负 = 'Y' then b.分值 else 0 end) 正分,
sum(case when b.正负 = 'N' then b.分值 else 0 end) 负分
from table1 a,
table2 b
where a.id = b.id(+)
group by a.部门
右关联就好了,你试下,谢谢!
with t1 as
(select 部门, sum(分值) as 正分
from 表二
where 正负="Y"
group by 部门) ,
t2 as
(select 部门, sum(分值) as 负分
from 表二
where 正负="N"
group by 部门)
select 表一.部门, t1.正分, t2.负分
from 表一
left join t1
on 表一.部门=t1.部门
inner join t2
on t1.部门=t2.部门
刚才忘记表一,修改了一下。
select t1.*,t2.正分,t2.负分 from 表一 t1,
(select 部门,sum(decode(正负,Y,分值,0)) as 正分,sum(decode(正负,N,分值,0)) as 负分
from 表二 group by 部门) t2 where t1.部门=t2.部门