oracle行列转换

使用sql语句实现?

行转列应用
score表及其数据如下:
STU COURSE SCORE
张三 数学 98
李四 数学 99
张三 语文 98
李四 语文 99
张三 物理 98
李四 物理 99
写SQL语句得到如下结果:
STU 数学 语文 物理
张三 98 98 98
李四 99 99 99

select stu,sum(decode(course,'数学',score,0)) 数学,sum(decode(course,'语文',score,0))语文,sum(decode(course,'物理',score,0)) 物理from score group by stu

select stu,
(case when COURSE = '数学' then SCORE end) 数学,
(case when COURSE = '语文' then SCORE end) 语文,
(case when COURSE = '物理' then SCORE end) 物理
from score
order by stu
;

网上有许多类似的,多查下。

--更正下
elect stu,
(case when COURSE = '数学' then SCORE end) 数学,
(case when COURSE = '语文' then SCORE end) 语文,
(case when COURSE = '物理' then SCORE end) 物理
from score
group by stu
;

如果你的数据库是oracle,应该用decode更过瘾。