问题描述
我在表中有两列。表名是使用内部联接和分组依据构造的,我们将此表称为Joined
。它有两列Present
和Score
。如果Present
为空,那么我想为Score
值赋0。
+------------+--------+-------------+------------+--------+
| Student_Id | Course | ExamDate | Present | Score |
+------------+--------+-------------+------------+--------+
| 1 | Math | 04/05/2020 | Yes | 45 |
| 2 | Math | 04/05/2020 | NULL | 90 |
| 2 | Math | 04/05/2020 | NULL | 50 |
+------------+--------+-------------+------------+--------+
我到目前为止是
SELECT DISTINCT StudentID,Course,ExamDate,Present,Score
CASE Present ISNULL
Score = 0
END
FROM Joined
我需要与众不同,因为内部联接可以使我重复一些。我需要的是
+------------+--------+-------------+------------+--------+
| Student_Id | Course | ExamDate | Present | Score |
+------------+--------+-------------+------------+--------+
| 1 | Math | 04/05/2020 | Yes | 45 |
| 2 | Math | 04/05/2020 | NULL | 0 |
+------------+--------+-------------+------------+--------+
对我来说,这感觉非常非常错误,但是我无法弄清楚如何通过一个查询来做到这一点。我该怎么办?
解决方法
您可以尝试以下-
SELECT DISTINCT StudentID,Course,ExamDate,Present,Score,CASE when Present IS NULL
then 0 else score END as scoreval
FROM Joined
,
如果Present为null,那么我想为Score值赋0。
case
表达式如下:
select
present,case when present is null
then 0
else score
end as score
from ...
当present
不是 null
时,您不知道该怎么办-因此,这将返回原始的score
。
不清楚您为什么需要distinct
。如果您要询问有关原始查询的问题,该查询似乎会产生(部分)重复项,则可以帮助解决该问题。