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 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用