Initial data: have a paragraph of query which in the sake of reasoning can be simplified to next
SELECT MAX((CASE
WHEN to_char(sy开发者_如何学Pythonsdate, 'DY') = 'SAT' THEN 1
WHEN to_char(sysdate, 'DY') = 'SUN' THEN 1
ELSE 0 END)) status
FROM mytable WHERE mycondition;
The problem: it appears that if mycondition results in empty result set first two when conditions don't work as they should and return null instead of 1. If I simply substitute the last line of query with next
FROM dual;
first two when conditions work flawless.
Condition: It should only be a query, no ifs, var defs etc.
How is it possible to perform this type of select but without a null value returned?
P.S.: Can use NVL/NVL2 but that really complicates the thing.
Thanks in advance.
Use a NVL function:
SELECT NVL(MAX((CASE
WHEN to_char(sysdate, 'DY') = 'SAT' THEN 1
WHEN to_char(sysdate, 'DY') = 'SUN' THEN 1
ELSE 0 END)), 1) status
FROM mytable WHERE mycondition;
CASE WHEN works fine, your problem is with MAX.
MAX and MIN return null when no rows match while SUM and COUNT return 0.
I'm afraid den-javamaniac is right, you need NVL, or perhaps a smart use of DECODE if you cannot switch out MAX.
精彩评论