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

解决方案 »

  1.   

    这样可以实现你现在所要的东东,但如果项目不一定的话就要用动态SQL语句来实现了
      

  2.   

    你要一句SQL,就只好这么办,除非你让我用视图什么的,因为里面有很多地方都是重复的,你仔细看就清楚了,这里关键是要把那几个栏位列出来,其他的都好办的
      

  3.   

    这是一个典型的关于行列转换的问题 可以运用decode()函数来进行解决给你一个 例子  自己照着他做一次你会收获很大SQL> select * from subject;STUDENTID SUBJECTID      SCORE
    --------- --------- ----------
    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
      

  4.   

    典型的行转列,如果列不固定,就动态sql了。
      

  5.   

    樓上說的都是固定的.如果人家的列是動態呢?這樣的問題,只能用報表工具實現如 cystal report, PowerBuilder
      

  6.   

    我这几天做的一个项目中就有这种问题,用SQL写还是不容易的,是在XSLT里边解析出来的.
      

  7.   

    晕,拜托别拿cystal report和 PowerBuilder相提并论……
    后者是开发工具。