SQL输出调整

时间:2016-03-16 18:34:45

标签: sql oracle

我有下表:

student  marks  subject
-------  -----  -------
AAA         67  ENG
AAA         78  MAT
CCC         88  SCI

我希望它转换如下:

student  eng  mat  sci
-------  ---  ---  ---
AAA       67   78     
CCC                 88

1 个答案:

答案 0 :(得分:1)

with dat as (
   select 'AAA' stud, 67 mk, 'ENG' subj from dual union all 
   select 'AAA' stud, 78 mk, 'MAT' subj from dual union all
   select 'CCC' stud, 88 mk, 'SCI' subj from dual )
SELECT *
FROM (SELECT stud, mk, subj from dat)
PIVOT (max(mk) for  (subj) in ('ENG' as eng, 'MAT' as mat, 'SCI' as sci)) 

STUD ENG MAT SCI
AAA  67  78 
CCC          88