已知一个表的结构为: 
姓名 科目 成绩 
张三 语文 20 
张三 数学 30 
张三 英语 50 
李四 语文 70 
李四 数学 60 
李四 英语 90 
怎样通过select语句把他变成以下结构: 
姓名 语文 数学 英语 
张三 20 30 50 
李四 70 60 90

解决方案 »

  1.   

    试下
    http://topic.csdn.net/u/20091019/11/67cd55a3-3f42-4db7-a3f8-91dd52a913cd.html?30915
      

  2.   

    列固定时
    select 姓名,
      max(decode(科目,'语文',成绩))语文,
      max(decode(科目,'数学',成绩))数学,
      max(decode(科目,'英语',成绩))英语,
    from table1
    group by 姓名
      

  3.   

    with tmp as(select '张三' names, '语文' code,20 grade from dual 
    union all select '张三','数学',30 from dual 
    union all select '张三','英语',50  from dual 

    select names,max(decode(rn,1,grade,0))语文, 
      max(decode(rn,2,grade,0))数学, 
     max(decode(rn,3,grade,0))英语 
    from(select tmp.*,row_number()over(partition by names order by grade)rn from tmp) 
    group by names 
    自己修改吧