2 (数据库题)有张学生成绩数据库表 studentscore,有以下字段
序号 字段名 类型 注释
1 ID Numeric(18,0) 学生证号
2 Name Varchar(20) 姓名
3 Kemu Varchar(20) 科目名称
4 Score Int 成绩
请写sql语句实现以下功能:
(1) 取科目名称为数学的学生成绩名次为第1名的学生记录。   
(2) 取科目名称为数学的学生成绩名次为第2名的学生记录。

解决方案 »

  1.   

    select top 1 * from studentscore where Kemu='数学' order by Score descselect top 1* from(
    select top 2 * from studentscore where Kemu='数学' order by Score desc
    ) a order by  Score 
      

  2.   

    (1)
    select top 1 with ties * from studentscore where 科目名称='数学' order by 成绩 desc(2)
    select top 1 with ties * from studentscore t
    where exists(select 1 from studentscore where 科目名称='数学' and 成绩>t.成绩)
      and 科目名称='数学' order by 成绩 desc
      

  3.   

    1.select top 1 * from studentscore where Kemu='数学' order by Score desc
    2.select
        *
      from
        (select  *,id=row_number()over(partition by kemu order by Score desc))t
      where
         Kemu='数学'  and id=2