[英]CASE WHEN with multiple THEN ELSE
Right now i get for reporting purposes the following data in 3 different columns : 现在,出于报告目的,我在3个不同的列中获得以下数据:
//Number
'CASE WHEN should_be.sh = "50"
THEN
REPLACE(SUBSTRING_INDEX(account.code,".",1)," ","")
ELSE
"500."
END',
// Account
'CASE WHEN should_be.sh = "50"
THEN
REPLACE(
SUBSTRING(SUBSTRING_INDEX(account.code,".",2),
INSTR(account.code, ".") + 1)
," ","")
ELSE
"92145000000"
END',
// Costsnumber
'CASE WHEN should_be.sh = "50"
THEN
CASE WHEN LENGTH(account.code) < 15
THEN
REPLACE(
SUBSTRING_INDEX(account.code,".",-1)
," ","")
ELSE
""
END
ELSE
""
END',
If every condition meets ELSE
, then the 3 columns look like that: 如果每个条件都符合ELSE
,则三列如下所示:
+--------+-------------+------------+
| number | account | costnumber |
+--------+-------------+------------+
| 500 | 92145000000 | |
+--------+-------------+------------+
How to combine the 3 columns, so I will have one column like that (for example in the ELSE case: 如何合并3列,所以我会有这样一列(例如,在ELSE情况下:
+-------------------------+
| combined |
+-------------------------+
| 500-92145000000-(empty) |
+-------------------------+
Just use concat_ws()
: 只需使用concat_ws()
:
CONCAT_WS('-',
(CASE WHEN should_be.sh = '50'
THEN REPLACE(SUBSTRING_INDEX(account.code, '.', 1), ' ', '')
ELSE '500.'
END),
(CASE WHEN should_be.sh = '50'
THEN REPLACE(SUBSTRING(SUBSTRING_INDEX(account.code, '.',2),
INSTR(account.code, '.') + 1), ' ', '')
ELSE '92145000000'
END),
(CASE WHEN should_be.sh = '50' AND LENGTH(account.code) < 15
THEN REPLACE(SUBSTRING_INDEX(account.code, '.', -1), ' ', '')
ELSE ''
END)
)
If you want "(empty)" to really appear for the third element, then replace the final ELSE ''
with ELSE '(empty)'
. 如果要在第三个元素中真正显示“(空)”,则用ELSE '(empty)'
替换最后的ELSE ''
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.