我看到使用CASE columnX WHEN NULL
如何设计查询
CASE columnX WHEN NOT NULL
?
SQL开发人员在查询上抛出错误,以便如何使用CASE进行条件查询... WHEN ...?
WITH dates_list AS
(SELECT TO_DATE('02-19-2018','MM-dd-yyyy') + ROWNUM - 1 AS DAY,
rownum AS row_num
FROM dual
CONNECT BY LEVEL <= (TO_DATE('03-29-2018','MM-dd-yyyy') - TO_DATE('02-19-2018','MM-dd-yyyy')+1)
)
SELECT dates1.day, dates2.*, count(case dates2.day when null then 1 else 0 end)
over (partition by dates2.day order by dates1.day) as cnt
FROM dates_list dates1
LEFT JOIN
(SELECT *
FROM dates_list
WHERE TO_CHAR(DAY,'D') NOT IN (7,1)
) dates2 ON dates1.day = dates2.day
ORDER BY dates1.day;
当我将when null
更改为when not null
时,以上查询会出错
答案 0 :(得分:4)
你不能以这种方式使用CASE句子和NULL值,因为columnX永远不会等于&#34;为NULL。您应该使用:
CASE WHEN columnX IS NULL THEN
或
CASE WHEN columnX IS NOT NULL THEN