面试题目

jackie_chen_520 2009-03-27 08:45:05
有一表结构如下:
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

谢谢各位啦~!!!
...全文
223 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiantom 2009-05-20
  • 打赏
  • 举报
回复
create table test_1
( id number,
name varchar2(20),
score number,
subject varchar2(20)
);
insert into test_1 values (1108,'cxj',86,'语文');
insert into test_1 values (1109,'cxj',76,'数学');
insert into test_1 values (1110,'cxj',56,'英语');
insert into test_1 values (1192,'xyz',74,'语文');
insert into test_1 values (1193,'xyz',71,'数学');
insert into test_1 values (1194,'xyz',72,'英语');
commit;
select * from test_1 t for update;
select t.name,
sum( decode(t.subject,'语文',t.score) ) 语文,
sum( decode(t.subject,'数学',t.score) ) 数学,
sum( decode(t.subject,'英语',t.score) ) 英语
from test_1 t group by t.name
bzcnc 2009-05-20
  • 打赏
  • 举报
回复
使用decode或者case语句都是能解决lz的问题的
culinapplefzu 2009-05-20
  • 打赏
  • 举报
回复
mark
welyngj 2009-05-20
  • 打赏
  • 举报
回复
up
lishan200012 2009-05-20
  • 打赏
  • 举报
回复
列转行
以前SQLSERVER上写过.
ORACLE上没有写过
j2eeoriented 2009-05-11
  • 打赏
  • 举报
回复
langcai1981 2009-04-17
  • 打赏
  • 举报
回复
这贴子可以结了,DECODE已经解决了问题了啊!
xiedurensheng 2009-04-17
  • 打赏
  • 举报
回复
select name,
max(decode(subject,'语文',score,0))语文,
max(decode(subject,'数学',score,0))数学,
max(decode(subject,'英语',score,0))英语
from grade group by name;

select name,
max(case when subject='语文' then score end)语文,
max(case when subject='数学' then score end)数学,
max(case when subject='英语' then score end)英语
from grade group by name;
feifei19850830 2009-04-07
  • 打赏
  • 举报
回复
用decode和组函数实现行列转换
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 temp_1 t
group by t.name

姓名 语文 数学 英语
cxj 86 76 56
xyz 74 71 72


还可以用case when 实现行列转换,一个道理
pyxyu 2009-04-07
  • 打赏
  • 举报
回复
select name 姓名,
(select score from temp1 b where b.Name=a.Name and b.subject='语文') 语文,
(select score from temp1 b where b.Name=a.Name and b.subject='数学') 数学,
(select score from temp1 b where b.Name=a.Name and b.subject='英语') 英语
from temp1 a group by name;
zhangyh_136 2009-03-31
  • 打赏
  • 举报
回复
是的,效率高为decode,效率低,支持所有数据库为choose case when else end choose
jdsnhan 2009-03-27
  • 打赏
  • 举报
回复
decode

1,619

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧