select 职工编号,姓名,
((select sum(2005年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) +
(select sum(2006年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号)+
(select sum(1994年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 调资额合计 ,
(select sum(2005年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 2005年调资金额 ,
(select sum(2006年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 2006年调资金额 ,
(select sum(1994年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 1994年调资金额
from cg a
((select sum(2005年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) +
(select sum(2006年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号)+
(select sum(1994年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 调资额合计 ,
(select sum(2005年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 2005年调资金额 ,
(select sum(2006年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 2006年调资金额 ,
(select sum(1994年调资金额) from (select 职工编号
(case when 待遇类别='2005年调资' then 调资金额 else 0 end ) as 2005年调资金额,
(case when 待遇类别=' 2006年调资' then 调资金额 else 0 end ) as 2006年调资金额,
(case when 待遇类别='1994年调资' then 调资金额 else 0 end ) as 1994年调资金额,
from cgdy b where b.职工编号=a.职工编号) as 1994年调资金额
from cg a
--------- --------- ----------
1 A1 82
1 A2 93
1 A3 91
2 A1 79
2 A2 81
3 A1 86
3 A3 76
4 A2 58
4 A3 779 rows selectedSQL> select studentid,A1,A2,A3,avg(A1+A2+A3) avg_score from
2 (
3 select studentid,sum(decode(subjectid,'A1',score,0)) A1,
4 sum(decode(subjectid,'A2',score,0)) A2,
5 sum(decode(subjectid,'A3',score,0)) A3
6 from subject
7 group by studentid
8 )
9 group by studentid,A1,A2,A3;STUDENTID A1 A2 A3 AVG_SCORE
--------- ---------- ---------- ---------- ----------
1 82 93 91 266
2 79 81 0 160
3 86 0 76 162
4 0 58 77 135
后者是开发工具。