gencong 2020-03-17 17:01 采纳率: 0%
浏览 447

SQLserver 分组排序如何优化?

有个学生表 , 存放每名学生每个月的语数英三科的成绩;
CREATE TABLE Student
(
Sid nvarchar(32) NOT NULL,
Sname nvarchar(32) NOT NULL,
Month datetime NULL,

Chinese_Score numeric(7, 2) NULL,
Math_Score numeric(7, 2) NULL,
English_Score numeric(7, 2) NULL
)
要取有这张表中, 连续两个月有同一科成绩都大于90分的学生。 由于学生表数据较大, 考虑用 lag 或 lead 函数做, 但是效率偏低。

麻烦各位大神看看有什么好的方法,帮忙解答下!!

  • 写回答

1条回答 默认 最新

  • 愤世嫉俗的胖子 2020-03-20 09:53
    关注

    select t1.sname
    from student t1, student t2
    where t1.sid = t2.sid and t1.month + 1 = t2.month
    and t1.chinese_score > 90 and t2.chinese_score > 90
    union
    select t1.sname
    from student t1, student t2
    where t1.sid = t2.sid and t1.month + 1 = t2.month
    and t1.math_score > 90 and t2.math_score > 90
    union
    select t1.sname
    from student t1, student t2
    where t1.sid = t2.sid and t1.month + 1 = t2.month
    and t1.english_score > 90 and t2.english_score > 90

    评论

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R