- 論壇徽章:
- 7
|
- 1. 非常規(guī)
- with tb (name,subject,result) as (
- select 'A','語文',74 from dual
- union all
- select 'A','數(shù)學',83 from dual
- union all
- select 'A','物理',93 from dual
- union all
- select 'B','語文',74 from dual
- union all
- select 'B','數(shù)學',84 from dual
- union all
- select 'B','物理',94 from dual
- ) select * from tb pivot(max(result) for subject in ('語文','數(shù)學','物理'));
- 2.常規(guī)
- with tb (name,subject,result) as (
- select 'A','語文',74 from dual
- union all
- select 'A','數(shù)學',83 from dual
- union all
- select 'A','物理',93 from dual
- union all
- select 'B','語文',74 from dual
- union all
- select 'B','數(shù)學',84 from dual
- union all
- select 'B','物理',94 from dual
- ) select name,
- max(case subject when '語文' then result else 0 end) 語文,
- max(case subject when '數(shù)學' then result else 0 end) 數(shù)學,
- max(case subject when '物理' then result else 0 end) 物理
- from tb
- group by name;
復制代碼 |
|