如何将以下源转换为以下目标

时间:2017-10-28 14:08:00

标签: oracle oracle11g

Student Name    Subject Name        Marks
Sam             Maths                100
Tom             Maths                 80
Sam             Physical Science      80
John            Maths                 75
Sam             Life Science          70
John            Life Science         100
John            Physical Science      85
Tom             Life Science         100
Tom             Physical Science      85

我们希望将目标表加载为:

Student Name    Maths   Life Science    Physical Science
Sam              100      70              80
John              75     100              85
Tom               80     100              85

1 个答案:

答案 0 :(得分:0)

试试这个。

SELECT student_name,
       "Maths",
       "Life Science",
       "Physical Science"
FROM
  (SELECT s.*
   FROM Student s ) 
PIVOT (MAX(Marks)
           FOR subject_name IN 
('Maths' AS "Maths",'Life Science' AS "Life Science",'Physical Science' AS "Physical Science") )
ORDER BY 3;