我有两张表,比如
学生:
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
输出在成绩列的第二行显示为null。但我希望它是F。
答案 0 :(得分: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,如果是oracle nvl,则应该使用ISNULL而不是func。查看其他人的this页面。
答案 1 :(得分:0)
您需要在案例陈述中处理Mark
NULL
:
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
答案 2 :(得分: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
答案 3 :(得分: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函数处理空值