大家好,问大家一个问题,假如现在我有张表A,字段包括学号、姓名、成绩,该表记录了每个学生一年每次考试的语文成绩,现在我想查出每个学生任何两次成绩相差10分以上的学生姓名,这样的SQL语句怎么写呢,谢谢大家了。

解决方案 »

  1.   

    with test as (
    select 001 as  学号 ,'张三' as 姓名,99 as 成绩 from dual
    union all 
    select 001 as  学号 ,'张三' as 姓名,11 as 成绩 from dual
    union all 
    select 002 as  学号 ,'张1' as 姓名,99 as 成绩 from dual
    union all 
    select 002 as  学号 ,'张1' as 姓名,94 as 成绩 from dual)
    select t.姓名
    from test t
    where exists (select * from test a where a.成绩-t.成绩>10)
     
      

  2.   

    任何两次成绩相差10分以上
    从另一角度思考,只要最高分与最低分相差10分就能满足了。
    简化了楼主的表
    with t as
     (select '小王' as name, 60 as score
        from dual
      union all
      select '小王' as name, 70 as score
        from dual
      union all
      select '大刘' as name, 80 as score
        from dual
      union all
      select '大刘' as name, 78 as score from dual)
    select name
      from (select name, max(score) - min(score)
              from t
             group by name
            having max(score) - min(score) >= 10);