如何显示一个null而不是0的行

时间:2013-11-08 22:20:06

标签: sql oracle null

我得到了正确的结果,但现在不是向我显示0的结果,而是希望它们向我显示null结果。我怎么能得到null而不是0?

SELECT w.FIRST_NAME,w.LAST_NAME,COUNT(s.SECTION_ID) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s 
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;

目前正在显示

FIRST_NAME                LAST_NAME                 COUNTED_SECTIONS
------------------------- ------------------------- ----------------
Rick                      Chow                                     0 
Marilyn                   Frantzen                                10 
Fernand                   Hanks                                    9 
Charles                   Lowry                                    0
etc

但我想要

FIRST_NAME                LAST_NAME                 COUNTED_SECTIONS
------------------------- ------------------------- ----------------
Rick                      Chow                                      
Marilyn                   Frantzen                                10 
Fernand                   Hanks                                    9 
Charles                   Lowry                                    
etc

我已经尝试过使用NVL并且无法正常工作

NVL(COUNT(s.SECTION_ID),NULL) AS COUNTED_SECTIONS

3 个答案:

答案 0 :(得分:2)

我认为NULLIF()在oracle中可用:

SELECT w.FIRST_NAME,w.LAST_NAME,NULLIF(COUNT(s.SECTION_ID),0) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s 
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;

答案 1 :(得分:1)

尝试nullif:

SELECT w.FIRST_NAME,w.LAST_NAME, NULLIF(COUNT(s.SECTION_ID), 0) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s 
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;

答案 2 :(得分:1)

您可以使用DECODE来决定要显示的内容

DECODE(COUNT(s.SECTION_ID),0, NULL, COUNT(s.SECTION_ID)) AS COUNTED_SECTIONS