2017-04-04 166 views
0

我無法弄清楚如何用'none'替換0。我在這裏做錯了什麼?使用NVL顯示'none'而不是0

SELECT s.student_id, s.first_name, s.last_name, NVL(TO_CHAR(COUNT(e.student_id)), 'none') AS enrollments 
FROM student s 
LEFT OUTER JOIN enrollment e 
ON s.student_id = e.student_id 
WHERE s.phone LIKE '%617%' 
GROUP BY s.student_id, s.first_name, s.last_name 
ORDER BY s.last_name, s.first_name; 
+1

NULL與數字0不同。COUNT返回0,而不是NULL。 NVL替換NULL,而不是數字0.這有幫助嗎? – mathguy

+0

如果這回答了您的問題,請將其標記爲接受的答案。否則,請提供更多詳情。 – SandPiper

回答

1

替換單個語句:

DECODE(COUNT(e.student_id), 0, 'none', COUNT(e.student_id)) AS enrollments

它所做的是它首先檢查計數;如果沒有適用的值,則COUNT將返回0.然後,它會將其解碼爲您的字符串。如果有記錄,它將返回COUNT函數。

相關問題