ITPUB论坛 » Oracle专题深入讨论 » 请教高手们一个 PLSQL 查询问题!
新一届的微软MVP评选已经开始,欢迎各位推荐!
2008-6-19 11:39 taps122
请教高手们一个 PLSQL 查询问题!

有个查询数据:
Stu_ID       Lesson      Score
080501      高数            82
080501      英语            84
080501      法律            78
080502      高数            76
080502      英语            84
080502      法律            89
....

想用 查询输出
Stu_ID      高数          英语             法律
080501     82            84               78
080502     76            84               89

之类的

用 Select 怎么写?

2008-6-19 11:57 晶晶小妹
行列转换吗,这种问题有太多例子可以参考了。可以使用CASE或Decode。

2008-6-19 14:40 wangfans
去ORACLE开发区

2008-6-20 10:34 taps122
已经解决!谢谢各位关注!

2008-6-24 13:15 zmjeffwc
select stu_id,decode(Lesson,'高数',Score)高数',decode(Lesson,'英语',Score)英语',decode(Lesson,'法律',Score)法律
from xxx
group by stu_id

2008-7-4 14:46 owlstudio
[quote]原帖由 [i]zmjeffwc[/i] 于 2008-6-24 13:15 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10757337&ptid=1008527][img]http://www.itpub.net/images/common/back.gif[/img][/url]
select stu_id,decode(Lesson,'高数',Score)高数',decode(Lesson,'英语',Score)英语',decode(Lesson,'法律',Score)法律
from xxx
group by stu_id [/quote]

[color=Blue]显然是错的!显然没有上机测试![/color]

正解:

select stu_id, sum(decode(Lesson,'高数',Score)) "高数", sum(decode(Lesson,'英语',Score))"英语",sum(decode(Lesson,'法律',Score))"法律"
from student group by stu_id;

2008-7-10 14:51 fuyunflying
对于列数确定得可以如此!
但是不确定列貌似可以使用存储过程

页: [1]


Powered by ITPUB论坛