3

每次它给我一个空结果时,我都试图得到一个结果“无”。现在它给我一个 0 的空结果。对于空结果,我怎么能有一行显示“无”而不是 0。

我已经尝试了 TO_CHAR 和 TO_NUMBER 的总和,但我无法让它显示“无”......

CASE WHEN SUM(ENROLLED) = 0 THEN 'none' ELSE SUM(ENROLLED) END AS ENROLLED

所以当尝试上面我得到 SQL 错误:ORA-00932:不一致的数据类型:预期的 CHAR 得到了 NUMBER

这就是我所拥有的

SELECT lt.STUDENT_ID,lt.FIRST_NAME, lt.LAST_NAME, CASE WHEN SUM(ENROLLED) = 0 THEN 'none' ELSE SUM(ENROLLED) END 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
4

4 回答 4

5

尝试使用函数:COALESCE (cast(sum(expr1) as varchar), 'none')

作为旁注,我质疑 DISTINCT 在您的查询中的使用。

于 2013-11-10T00:49:55.520 回答
3
CASE WHEN SUM(ENROLLED) = 0 THEN 'none' ELSE SUM(ENROLLED) END AS ENROLLED

这将返回不同的类型。使其相同(将 SUM 转换为字符串)

于 2013-11-10T00:50:44.010 回答
1

该错误是正常的,因为您的命令

案例当 SUM(ENROLLED) = 0 THEN 'none' ELSE SUM(ENROLLED) END AS ENROLLED

使用与字符串 'none' 和数字 SUM(ENROLLED) 相同的列

您可以将列用作字符串并使用同时显示数字

CASE WHEN SUM(ENROLLED) = 0 THEN 'none' ELSE TO_CHAR (SUM(ENROLLED)) END AS ENROLLED

于 2014-09-21T12:47:34.740 回答
0

尝试这个:

COALESCE(to_char(sum(expr1)), 'none')

这应该可以在不使用 cast 和 varchar 的情况下工作。

于 2017-10-16T06:37:12.327 回答