查看: 7112|回复: 12

[原创] 【讨论】请教一个SQL语句的问题

[复制链接]
论坛徽章:
5
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33优秀写手
日期:2015-02-12 06:00:142015年新春福章
日期:2015-03-04 14:19:112015年新春福章
日期:2015-03-06 11:57:31
跳转到指定楼层
1#
发表于 2015-1-6 13:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
大概的业务需求是这样的:一个学生(student_id)有很多课程(class_id)、每门课程有一个得分(score)。现在想输出每个学生得分最高的那门课程,请问如何用一个SQL实现(Postgresql)。谢谢!

create table my_test(
  id           bigserial,
  student_id   int,
  class_id     varchar(64),
  score        int
);


delete from my_test;
insert into my_test(student_id, class_id, score) values (1, 'c1', 99);
insert into my_test(student_id, class_id, score) values (1, 'c2', 90);
insert into my_test(student_id, class_id, score) values (1, 'c3', 87);

insert into my_test(student_id, class_id, score) values (2, 'c2', 99);
insert into my_test(student_id, class_id, score) values (2, 'c1', 90);
insert into my_test(student_id, class_id, score) values (2, 'c3', 87);

insert into my_test(student_id, class_id, score) values (3, 'c3', 99);
insert into my_test(student_id, class_id, score) values (3, 'c1', 90);
insert into my_test(student_id, class_id, score) values (3, 'c2', 87);


期望的输出为:
1, 'c1', 99
2, 'c2', 99
3, 'c3', 99
论坛徽章:
2
2015年新春福章
日期:2015-03-04 14:53:162015年新春福章
日期:2015-03-06 11:58:39
2#
发表于 2015-1-6 14:46
select student_id,class_id,max(score)
from my_test
group by  student_id

论坛徽章:
2
2015年新春福章
日期:2015-03-04 14:53:162015年新春福章
日期:2015-03-06 11:58:39
3#
发表于 2015-1-6 14:58 | 只看该作者
select student_id,class_id,max(score)
from my_test
group by  student_id

使用道具 举报

回复
论坛徽章:
12
优秀写手
日期:2014-09-03 06:00:13双鱼座
日期:2015-12-16 10:37:41天蝎座
日期:2015-12-02 14:44:59秀才
日期:2015-11-11 09:58:34ITPUB14周年纪念章
日期:2015-10-26 17:23:44巨蟹座
日期:2015-09-02 14:45:30金牛座
日期:2015-08-25 16:02:53天蝎座
日期:2015-07-21 16:49:04喜羊羊
日期:2015-07-07 16:42:312015年新春福章
日期:2015-03-06 11:59:47
4#
发表于 2015-1-6 14:58 | 只看该作者
业余选手献上一个,大侠有更好的写出来学习下了
select a.student_id,a.class_id,a.score from my_test a right join
(select student_id,max(score) score from my_test group by student_id) b
on a.student_id=b.student_id and a.score=b.score;

使用道具 举报

回复
论坛徽章:
12
优秀写手
日期:2014-09-03 06:00:13双鱼座
日期:2015-12-16 10:37:41天蝎座
日期:2015-12-02 14:44:59秀才
日期:2015-11-11 09:58:34ITPUB14周年纪念章
日期:2015-10-26 17:23:44巨蟹座
日期:2015-09-02 14:45:30金牛座
日期:2015-08-25 16:02:53天蝎座
日期:2015-07-21 16:49:04喜羊羊
日期:2015-07-07 16:42:312015年新春福章
日期:2015-03-06 11:59:47
5#
发表于 2015-1-6 15:00 | 只看该作者
fxl201001 发表于 2015-1-6 14:58
select student_id,class_id,max(score)
from my_test
group by  student_id

这样不行啊,class_id不一定是最高分那个

使用道具 举报

回复
论坛徽章:
5
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33优秀写手
日期:2015-02-12 06:00:142015年新春福章
日期:2015-03-04 14:19:112015年新春福章
日期:2015-03-06 11:57:31
6#
 楼主| 发表于 2015-1-6 15:00 | 只看该作者
fxl201001 发表于 2015-1-6 14:58
select student_id,class_id,max(score)
from my_test
group by  student_id

这个不对:
[Err] ERROR:  column "my_test.class_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 4: select student_id,class_id,max(score)

使用道具 举报

回复
论坛徽章:
2
2015年新春福章
日期:2015-03-04 14:53:162015年新春福章
日期:2015-03-06 11:58:39
7#
发表于 2015-1-6 15:08 | 只看该作者
不好意思,看成mysql 了

使用道具 举报

回复
论坛徽章:
5
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33优秀写手
日期:2015-02-12 06:00:142015年新春福章
日期:2015-03-04 14:19:112015年新春福章
日期:2015-03-06 11:57:31
8#
 楼主| 发表于 2015-1-6 15:13 | 只看该作者
fuiou_cary 发表于 2015-1-6 14:58
业余选手献上一个,大侠有更好的写出来学习下了
select a.student_id,a.class_id,a.score from my_test a  ...

这个行,但是似乎扫描了两次表。是否有更高效的算法?

使用道具 举报

回复
求职 : 数据库开发
论坛徽章:
41
2017金鸡报晓
日期:2017-02-08 14:09:13秀才
日期:2016-01-13 12:14:26秀才
日期:2016-01-12 11:23:27金牛座
日期:2016-01-03 20:58:56秀才
日期:2015-12-21 09:53:46秀才
日期:2015-12-21 09:48:11秀才
日期:2015-12-18 09:28:57秀才
日期:2015-12-14 15:02:13秀才
日期:2015-11-23 09:48:22秀才
日期:2016-01-21 13:37:04
9#
发表于 2015-1-6 16:13 | 只看该作者
fxl201001 发表于 2015-1-6 14:58
select student_id,class_id,max(score)
from my_test
group by  student_id

这个sql你自己跑过?

使用道具 举报

回复
论坛徽章:
2
2015年新春福章
日期:2015-03-04 14:53:162015年新春福章
日期:2015-03-06 11:58:39
10#
发表于 2015-1-6 16:15 | 只看该作者
在 mysql 下跑过,我以为是  mysql 了 。

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表