在Oracle中使用PIVOT运算符

时间:2016-12-15 06:51:43

标签: sql oracle pivot

我使用像这样的枢轴

SELECT * FROM
(
  SELECT STUDENT_NAME,subject_code ,TERM_I_GRADE  FROM tablename
)
PIVOT
(
 TERM_I_GRADE 
  FOR subject_code IN (2001,2002)
)
ORDER BY STUDENT_NAME;
std_code Student_Name  subject_code    TERM_I_GRADE  
9        Kishan        2003            A
9        Kishan        2001            A
9        Kishan        2002            A
9        Kishan        2007            A
9        Kishan        2004            A

output 
-------------------
stdcode Student_Name  2003  2001  2002  2007 2004 
9       Kishan        A      A    A     A    A

2 个答案:

答案 0 :(得分:2)

select  *
from    mytable pivot (max(TERM_I_GRADE) for subject_code in (2003,2001,2002,2007,2004)) p
+----------+--------------+------+------+------+------+------+
| STD_CODE | STUDENT_NAME | 2003 | 2001 | 2002 | 2007 | 2004 |
+----------+--------------+------+------+------+------+------+
| 9        | Kishan       | A    | A    | A    | A    | A    |
+----------+--------------+------+------+------+------+------+

答案 1 :(得分:0)

SELECT YEAR_CODE,' HAVALGA' AS School_Name,class_code,STUDENT_CODE,STUDENT_NAME,KANNADA,ENGLISH,HINDI,MATHS,SCIENS,SOCIAL,EVS来自 (选择         YEAR_CODE,class_code,STUDENT_CODE,STUDENT_NAME,         SUBJECT_CODE,TERM_I_GRADE为TERM_I_GRADE 来自SRSDF_EXAM_TERM_I WHERE
      SCHOOL_CODE = 1301和YEAR_CODE =' 2016-2017'

) PIVOT(MAX(TERM_I_GRADE)FOR     SUBJECT_CODE IN(2001 AS KANNADA,2002 AS ENGLISH,                      2003 AS HINDI,2004 AS MATHS,                      2005 AS ASS,2006 AS SOCIAL,                      2007 AS EVS))          ORDER BY class_code,STUDENT_CODE;