我正在使用 CASE 清理表格中的一些州缩写,但它的工作方式与逻辑相反。我单独选择了长度以表明长度计算正确,所以我认为这是关闭的 CASE 逻辑
当我查询...
SELECT billing_state,
length(billing_state),
CASE billing_state
WHEN length(billing_state) > 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
WHEN length(billing_state) = 2 THEN upper(billing_state)
ELSE 'UNKNOWN'
END as billing_state_fixed
FROM accounts
+---------------+-----------------------+---------------------+
| billing_state | length(billing_state) | billing_state_fixed |
+---------------+-----------------------+---------------------+
| GA | 2 | NULL |
| Alabama | 7 | ALABAMA |
| MS | 2 | NULL |
| FL | 2 | NULL |
| NULL | NULL | UNKNOWN |
+---------------+-----------------------+---------------------+
然而,当我进入这个奇怪的逻辑时,它就起作用了。
SELECT billing_state,
length(billing_state),
CASE billing_state
WHEN length(billing_state) = 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
WHEN length(billing_state) <> 2 THEN upper(billing_state)
ELSE 'UNKNOWN'
END as billing_state_fixed
FROM accounts
+---------------+-----------------------+---------------------+
| billing_state | length(billing_state) | billing_state_fixed |
+---------------+-----------------------+---------------------+
| GA | 2 | GA |
| Alabama | 7 | AL |
| MS | 2 | MS |
| FL | 2 | FL |
| NULL | NULL | UNKNOWN |
+---------------+-----------------------+---------------------+
有人可以试试这个吗?