由于报表的需要,小弟需要将oracle数据表中的行数据转换为列,不知道哪位高人有这方面的经历,请给点建议

解决方案 »

  1.   

    我知道固定列标题的转法,列标题不固定的不会(SQL SERVER中会)
    如表字段test(f1 varchar,f2 bunmer)
    内容为:f1    f2
          A      10
          B      15
          C      11
    转换成列为:A   B    C
              10  15   11程序为:
     select sum(case when f1 = 'A' then f2 else 0 end) A,
            sum(case when f1 = 'B' then f2 else 0 end) B,
            sum(case when f1 = 'C' then f2 else 0 end) C
       from test
      group by f1;
      

  2.   

    姓名 科目 成绩 
    张三 语文 20 
    张三 数学 30 
    张三 英语 50 
    李四 语文 70 
    李四 数学 60 
    李四 英语 90
    转换成:姓名 语文 数学 英语 
           张三 20   30   50 
          李四 70   60   90
    SQL> select name,
             2  sum(decode(subject,'语文',grade,0))yuwen,
             3  sum(decode(subject,'数学',grade,0))shuxue,
             4  sum(decode(subject,'英语',grade,0))yingyu
             5  from test group by name;
      

  3.   

    用分析函数or 自己编一个function