计算sql server 2008中学生的成绩

时间:2015-01-09 10:06:55

标签: sql-server-2008

对于下表

SQL> create table stud( sno int primary key, sname char(15), sub1 int,sub2 int, sub3 int,grade char(15));
SQL> insert into stud values(1,'Pray',98,94,90,' ');
SQL> insert into stud values(2,'Jay',57,74,40,' ');
SQL> insert into stud values(3,'Prisha',58,54,50,' ');
SQL> insert into stud values(4,'Masum',48,44,40,' ');
SQL> insert into stud values(5,'Shyam',40,40,40,' ');

select * from stud;的输出结果为

SNO   SNAME              SUB1       SUB2       SUB3    GRADE
----- --------------- ---------- ---------- ---------- ---------------
1     Pray                98         94         90       
2     Jay                 57         74         40       
3     Prisha              58         54         50       
4     Masum               48         44         40       
5     Shyam               40         40         40       

此格式的输出能否自动提供哪些学生通过或失败? select * from stud;

SNO   SNAME              SUB1       SUB2       SUB3    GRADE
----- --------------- ---------- ---------- ---------- ---------------
1     Pray                98         94         90      Distinction
2     Jay                 57         74         40      Pass
3     Prisha              58         54         50      Pass
4     Masum               48         44         40      Fail
5     Shyam               40         40         40      Fail

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

SELECT SNO
,SNAME
,SUB1
,SUB2
,SUB3
,CASE 
    WHEN SUB1 < 50
        OR SUB2 < 50
        OR SUB3 < 50
        THEN 'Fail'
    WHEN (
            SUB1 BETWEEN 50
                AND 70
            )
        OR (
            SUB2 BETWEEN 50
                AND 70
            )
        OR (
            SUB3 BETWEEN 50
                AND 70
            )
        THEN 'Pass'
    WHEN SUB1 >= 70
        OR SUB2 >= 70
        OR SUB3 >= 70
        THEN 'Distinction'
    END AS GRADE
FROM STUD

您可以使用相同的逻辑来更新成绩列。

答案 1 :(得分:0)

我不完全确定你想要什么,但我会选择这样的东西:

select SNO, SNAME, SUB1, SUB2, SUB3, GRADE =
case when (sub1 + sub2 + sub3)/3 >= 90 then 'Distinction'
     when (sub1 + sub2 + sub3)/3 < 50 then 'Fail'
     else 'Pass'
end
from stud;