我试图在脚本下运行,并且只能获得有“!,@和#”的记录。但是,为什么会出现空记录?
SELECT
CASE WHEN NAME LIKE ('%!%') THEN '!'
WHEN NAME LIKE ('%@%') THEN '@'
WHEN NAME LIKE ('%#%') THEN '#'
END AS SPECIAL_CHARACTERS, COUNT (1) AS TOTAL
FROM
PERSONAL_DETAILS
GROUP BY
CASE WHEN NAME LIKE ('%!%') THEN '!'
WHEN NAME LIKE ('%@%') THEN '@'
WHEN NAME LIKE ('%#%') THEN '#'
END
特殊字符
(null) 227783
! 8
@ 13
# 9
答案 0 :(得分:2)
您的CASE
没有ELSE
,因此我们会分配不匹配NULL
。
如果您不想要它们,请在WHERE
子句中使用过滤器:
SELECT (CASE WHEN NAME LIKE '%!%' THEN '!'
WHEN NAME LIKE '%@%' THEN '@'
WHEN NAME LIKE '%#%' THEN '#'
END) AS SPECIAL_CHARACTERS,
COUNT(*) AS TOTAL
FROM PERSONAL_DETAILS
WHERE NAME LIKE '%!%' OR NAME LIKE '%@%' OR NAME LIKE '%#%'
GROUP BY (CASE WHEN NAME LIKE '%!%' THEN '!'
WHEN NAME LIKE '%@%' THEN '@'
WHEN NAME LIKE '%#%' THEN '#'
END);
注意:
LIKE
模式周围不需要括号。WHERE
条件可以替换为REGEXP_LIKE(NAME< '[!@#]')
。