有一表结构如下:
id name score subject
1108 cxj 86 语文
1109 cxj 76 数学
1110 cxj 56 英语
1192 xyz 74 语文
1193 xyz 71 数学
1194 xyz 72 英语
写sql求结果:
姓名 语文 数学 英语
cxj 86 76 56
xyz 74 71 72
create table test(
id varchar2(4),
name varchar2(3),
score varchar2(8),
subject varchar2(8)
);
insert into test values('1108','cxj',86,'语文');
insert into test values('1109','cxj',76,'数学');
insert into test values('1110','cxj',56,'英语');
insert into test values('1192','xyz',74,'语文');
insert into test values('1193','xyz',71,'数学');
insert into test values('1194','xyz',72,'英语');
select t.name "姓名",
max(decode(t.subject,'语文',t.score,0)) "语文",
max(decode(t.subject,'数学',t.score,0)) "数学",
max(decode(t.subject,'英语',t.score,0)) "英语"
from test t
group by t.name
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21220558/viewspace-590844/ ,如需转载,请注明出处,否则将追究法律责任。