create table sc(
student_id char(10),
course_id char(5),
sc_time datetime,
score decimal(3)
在上面的选课表中,允许学生可以在不同的学期选择相同的一门课程(如补考等),
但如何选择出最后的那次成绩?当然创建一个视图可以满足create view last_sc
as
select student_id, course_id, max(sc_time)
from sc
group by student_id, course_id;create view last_sc_score
as
select student_id, course_id, grade
from sc left join last_sc
on sc.student_id = last_sc.student_id and sc.course_id=last_sc.student_id
可这样毕竟笨了,有么样更简便或新鲜的方法呢?忘大家指教!
解决方案 »
- 求教:Enterprise linux 5上安装oracle 10g出现的问题
- oracle10G一个用户指向了两个表空间。
- oracle sqlldr 报错:SQL*Loader-350
- 求更新数据库前N条记录的语句
- [非常急]虚拟机上安装9i执行部分SQL时报ora-03113错误
- 请教关于oracle8I中从noarchive到archive时出现的问题???????????//
- rman应该怎么用?
- 安装Oracle重启后,为何系统登录不进去?(在线等待)
- 初学问题
- oracle存储过程想实现一个时间区间内进行多次计算得到值
- 求一语句:oracle 删除表中其中一部分数据,仅保留原来1/10的数据,怎么删除现有的记录?
- 将Sybase的数据导入到ORACLE中遇到的问题
大家理会意思即可create view last_sc(student_id, course_id, max_sc_time)
as
select student_id, course_id, max(sc_time)
from sc
group by student_id, course_id;create view last_sc_score
as
select student_id, course_id, grade
from sc left join last_sc
on sc.student_id = last_sc.student_id and sc.course_id=last_sc.student_id and sc_time=max_sc_time
既然不同学期可以选择同一门课程,那就应该在表中增加一个字段,表示学期。
select student_id ,course_id ,sc_time ,score ,
row_number() over(partition by student_id ,course_id order by sc_time desc) rn
) where rn=1
FROM sc
WHERE (student_id, course_id, sc_time) IN (
SELECT student_id, course_id,
MAX (sc_time) TIME
FROM sc
GROUP BY student_id, course_id)
select t.* from sc t where sc_time = (select max(sc_time) from sc where student_id = t.student_id) order by t.student_idselect t.* from sc t where not exists (select 1 from sc where student_id = t.student_id and sc_time > t.sc_time) order by t.student_id