0

我有两张桌子

学生:

roll    Name
1       A
2       B
3       C

标记

roll    Mark
1       85
3       95

我想要一个像

roll    Name    Grade
1       A       A
2       B       F
3       C       A+

我试过这个查询

select Stdent.Roll,Stdent.Name,Grade= case 
when (Mark between 0 and 79) then ('F')
when (Mark between 80 and 89) then ('A')
when (Mark between 90 and 100) then ('A+') end  
from Mark 
right join Stdent 
on Mark.roll=Stdent.Roll 
order by Stdent.Roll

输出在成绩列的第 2 行显示为空。但我希望它是 F。

4

4 回答 4

1
select Stdent.Roll,Stdent.Name,Grade= case 
when (func(Mark,0) between 0 and 79) then ('F')
when (Mark between 80 and 89) then ('A')
when (Mark between 90 and 100) then ('A+') end  
from Mark 
right join Stdent 
on Mark.roll=Stdent.Roll 
order by Stdent.Roll

如果您使用的是 sql server,则应该使用 ISNULL 而不是 func,如果是 oracle nvl。检查页面以了解其他人。

于 2013-01-07T11:35:28.893 回答
0

您需要处理您MarkNULL案例陈述:

SELECT  Student.Roll,
        Student.Name,
        Grade = CASE 
                    WHEN Mark.Mark BETWEEN 0 AND 79 OR Mark.Mark IS NULL THEN 'F'
                    WHEN Mark.Mark BETWEEN 80 AND 89 THEN 'A'
                    WHEN Mark.Mark BETWEEN 90 AND 100 THEN 'A+'
                END  
FROM    Mark 
        RIGHT JOIN Student 
            ON Mark.roll=Student.Roll 
ORDER BY Student.Roll
于 2013-01-07T11:37:22.327 回答
0

尝试这个:

select Stdent.Roll,Stdent.Name,Grade= case 
when (IsNull(Mark, 0) between 0 and 79) then ('F')
when (Mark between 80 and 89) then ('A')
when (Mark between 90 and 100) then ('A+') end  
from Mark 
right join Stdent 
on Mark.roll=Stdent.Roll 
order by Stdent.Roll
于 2013-01-07T11:37:50.467 回答
0
select Stdent.Roll,Stdent.Name,Grade= case 
 when (NVL(Mark,0) between 0 and 79) then ('F')
 when (Mark between 80 and 89) then ('A')
 when (Mark between 90 and 100) then ('A+') end  
from Mark 
 right join Stdent 
 on Mark.roll=Stdent.Roll 
order by Stdent.Roll

使用 NVL 函数处理空值

于 2013-01-07T11:52:56.307 回答