有一个学生成绩表,里面有各科的成绩,现在请查询并显示出学生姓名,科目名,及其学科状态(score>80[优秀],60<score<80[及格],score<60[不及格])。

解决方案 »

  1.   

    select 学生姓名,(case when 语文>=80 then '优秀'
    when 语文>=60 then '及格'
    else '不及格') as 语文,
    (case when 数学>=80 then '优秀'
    when 数学>=60 then '及格'
    else '不及格') as 数学,
    (case when 英语>=80 then '优秀'
    when 英语>=60 then '及格'
    else '不及格') as 英语,
    from table
      

  2.   


    select 学生姓名,科目名,
    case when score>=80 then '优秀'
    when score>=60 and score<80 then '及格'
    else '不及格' end as 学科状态
    from 学生成绩表
      

  3.   

    select 学生姓名,科目名,
    (case when score>80 then '优秀' when score>=60 and score<80 then '及格'
    else '不及格' end) as 学科状态 from 学生成绩表
      

  4.   

    decode函数
    或者case when
      

  5.   

    DECODE(value, if1, then1, if2,then2, if3,then3, . . . else ) 比较简洁
      

  6.   


    能不能用decode写具体点,我之前从来没有用过decode啊?
      

  7.   


    select 学生姓名,科目名, decode(trunc((score-40)/20),0,'不及格',1,'及格','优秀') as 学科状态 from 学生成绩表 
      

  8.   

    用 case when 也是一样的、
      

  9.   

    目前认为表结构为(学生姓名,课程名,成绩)
    参考代码如下:SELECT 学生姓名,科目名,
    (CASE WHEN 成绩>80 THEN '优秀' 
          WHEN 成绩>=60 AND 成绩<80 THEN '及格'
          ELSE '不及格' END) as 学科状态
    FROM 表名;
    注意:不能使decode,因为decode无法判断范围,它只能与离散的具体值做比较。
      

  10.   


    可以的 不过有点麻烦 不如用case when select 学生姓名,科目名, decode(sign(score-20),-1,'不及格',decode(trunc((score-40)/20),0,'不及格',1,'及格','优秀')) as 学科状态  from 学生成绩表