Snowflake中case when语句按不同列拼接值报Unexpected as错误如何解决
问题解答
错误原因
本次报错由两个语法问题共同导致:
- 代码中第4个
CASE WHEN判断分支缺少END闭合标签,语法解析无法识别后续语句,直到遇到as关键字时触发异常 - Snowflake不支持使用
+运算符做字符串拼接,该运算符仅可用于数值计算,字符串拼接需要使用||运算符或者内置拼接函数
另外你当前的逻辑生成的字符串会自带开头的逗号,和你期望的/分隔的输出效果不匹配,也需要同步调整。
修复方案
使用Snowflake内置的CONCAT_WS函数实现按分隔符拼接,该函数会自动忽略空值,无需额外处理多余分隔符,调整后代码如下:
select *, case when checkouttime is not null and primarypatientinsuranceid is not null and closedby is not null and billingtabcheckeddate is not null and alreadyrouted is not null then 'Valid Missing slip' else CONCAT_WS( '/', case when checkouttime is null then 'Patient is not checked out' end, case when primarypatientinsuranceid is null then 'No insurance information' end, case when closedby is null then 'Encounter not signed off' end, case when billingtabcheckeddate is null then 'Billing tab is not checked' end, case when alreadyrouted is null then 'Missing slip already routed' end ) end as resultant from final
调整说明
- 修复了
CASE WHEN缺少闭合标签的语法错误,每个判断分支独立处理 - 替换了错误的
+拼接方式,CONCAT_WS第一个参数指定分隔符为/,后续参数为待拼接内容,自动跳过NULL值,不会生成多余分隔符 - 新增全量非空判断,所有校验列都不为空时直接输出
Valid Missing slip,完全匹配你给出的预期输出效果
内容的提问来源于stack exchange,提问作者user3369545
相关产品推荐
相关产品推荐

