我每次给它一个null结果时都试图得到'none'的结果。现在它给我一个0表示空结果。我怎么能有一行显示'无'而不是0表示空结果。
我已经尝试过NVL(SUM(已注册),'无')已注册并且无法正常工作
SELECT DISTINCT lt.STUDENT_ID,lt.FIRST_NAME, lt.LAST_NAME, NVL(SUM(ENROLLED),'none') AS ENROLLED
FROM STUDENT lt
LEFT OUTER JOIN
(SELECT s.STUDENT_ID, e.ENROLL_DATE,COUNT(z.COURSE_NO) AS ENROLLED
FROM STUDENT s
LEFT JOIN ENROLLMENT e ON s.STUDENT_ID = e.STUDENT_ID
LEFT JOIN SECTION z ON e.SECTION_ID = z.SECTION_ID
WHERE s.PHONE LIKE '702%'
GROUP BY s.STUDENT_ID, e.ENROLL_DATE) rt
ON lt.STUDENT_ID = rt.STUDENT_ID
WHERE lt.PHONE LIKE '702%'
GROUP BY lt.STUDENT_ID,lt.FIRST_NAME, lt.LAST_NAME,ENROLLMENTS;
现在我正在
STUDENT_ID FIRST_NAME LAST_NAME ENROLLED
---------- ------------------------- ------------------------- -----------
253 Walter Boremmann 1
396 James E. Norman 0
etc
但我想要
STUDENT_ID FIRST_NAME LAST_NAME ENROLLED
---------- ------------------------- ------------------------- -----------
253 Walter Boremmann 1
396 James E. Norman none
答案 0 :(得分:1)
您似乎将null
值与0
混合在一起。他们不一样。如果第一个表达式不是NVL
,则null
将返回第一个表达式,如果第一个表达式为null
,则返回第二个表达式。由于0
不是null
,因此您将始终获得SUM(ENROLLED)
。您应该使用0
进行比较。
只需改变一下:
NVL(SUM(ENROLLED),'none') AS ENROLLED
进入这个:
CASE WHEN SUM(ENROLLED) = 0 THEN 'none' ELSE SUM(ENROLLED) END AS ENROLLED