/*
在實際使用sql工作中總會碰到將某一列的值放到標題中顯示
比如有如下數據:
ID NAME KECHENG CHENGJI
那末我要求顯示的結果是:
NAME YUWEN SHUXUE
a
也就是說把課程這一列放到行上顯示
我只介紹
首先建立表:
*/
create table fzq
(
id varchar(
name varchar(
kecheng varchar(
chengji varchar(
);
insert into fzq values (
insert into fzq values(
insert into fzq values (
insert into fzq values (
insert into fzq values (
insert into fzq values (
/*首先使用union
select name
(
select name
where kecheng=
select name
from fzq
where kecheng=
) aaa
group BY name;
/*執行結果:
NAME YUWEN SHUXUE
a
b
c
*/
/*
其次是用case
*/
select name
sum(case kecheng when
from fzq
group by name;
/*執行結果:
NAME YUWEN SHUXUE
a
b
c
所有例子在oracle中測試
*/
select name
sum(decode(kecheng
sum(decode(kecheng
from fzq
group by name
From:http://tw.wingwit.com/Article/program/Oracle/201311/18036.html