A D*_*Doe 0 sql sqlite case sql-null
我的表中有两列。表名是用inner join和group by构造的,我们称这个表为Joined。它有两列Present和Score。如果Present为 null,那么我想将 0 赋给该Score值。
+------------+--------+-------------+------------+--------+
| 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 |
+------------+--------+-------------+------------+--------+
Run Code Online (Sandbox Code Playgroud)
我现在所拥有的是
SELECT DISTINCT StudentID ,Course, ExamDate, Present, Score
CASE Present ISNULL
Score = 0
END
FROM Joined
Run Code Online (Sandbox Code Playgroud)
我需要不同的,因为内部联接可以给我一些重复。我需要的是
+------------+--------+-------------+------------+--------+
| Student_Id | Course | ExamDate | Present | Score |
+------------+--------+-------------+------------+--------+
| 1 | Math | 04/05/2020 | Yes | 45 |
| 2 | Math | 04/05/2020 | NULL | 0 |
+------------+--------+-------------+------------+--------+
Run Code Online (Sandbox Code Playgroud)
这对我来说感觉非常非常错误,但我一直无法弄清楚如何通过一个查询来做到这一点。我该怎么做?
\n\n如果 Present 为 null,那么我想将 Score 值指定为 0。
\n
表达式case如下:
select \n present,\n case when present is null \n then 0 \n else score \n end as score\nfrom ...\nRun Code Online (Sandbox Code Playgroud)\npresent您不\xe2\x80\x99t 告诉在不是时 要做什么null- 所以这会返回原始的score.
目前尚不清楚为什么您需要distinct. 如果您要询问有关原始查询的问题,该查询似乎会产生(部分)重复项,那么有人可能会帮助修复它。
| 归档时间: |
|
| 查看次数: |
1134 次 |
| 最近记录: |