我有一个适合你的基本款。我在下面的语句中打破了语法。我不知道如何在一个语句中运行两个'case'选项。我收到错误:找不到FROM关键字。
SELECT
CASE
WHEN`enter code here`
attr.UDA_AUTO_KEY = 40
THEN
'Parts Sales'
ELSE
'This is blank'
END AS Parts Sales,
CASE
WHEN
attr.UDA_AUTO_KEY = 5
THEN
'Aircraft Management'
ELSE
'This is blank'
END AS Aircraft Management,
cmp.COMPANY_NAME, cmp.ADDRESS1
FROM COMPANIES cmp
LEFT JOIN UDA_CHECKED uda ON uda.AUTO_KEY = cmp.CMP_AUTO_KEY
LEFT JOIN USER_DEFINED_ATTRIBUTES attr ON uda.UDA_AUTO_KEY = attr.UDA_AUTO_KEY
WHERE attr.AUTO_KEY_PREFIX = 'CMP'
答案 0 :(得分:2)
问题不在CASE
中,而在列别名中。
如果您需要带空格的别名,则必须使用双引号:
SELECT
CASE
WHEN
attr.UDA_AUTO_KEY = 40
THEN
'Parts Sales'
ELSE
'This is blank'
END AS "Parts Sales",
CASE
WHEN
attr.UDA_AUTO_KEY = 5
THEN
'Aircraft Management'
ELSE
'This is blank'
END AS "Aircraft Management",
...
否则,只需删除空格:
SELECT
CASE
WHEN
attr.UDA_AUTO_KEY = 40
THEN
'Parts Sales'
ELSE
'This is blank'
END AS PartsSales,
CASE
WHEN
attr.UDA_AUTO_KEY = 5
THEN
'Aircraft Management'
ELSE
'This is blank'
END AS AircraftManagement,
...
答案 1 :(得分:0)
更新
使用双引号"
汇总列别名。
整个声明:
SELECT
CASE WHEN attr.UDA_AUTO_KEY = 5 THEN 'Aircraft Management'
ELSE 'This is blank' END AS "Aircraft Management",
CASE WHEN attr.UDA_AUTO_KEY = 40 THEN 'Parts Sales'
ELSE 'This is blank' END AS "Parts Sales",
cmp.COMPANY_NAME,
cmp.ADDRESS1
FROM COMPANIES cmp
LEFT JOIN UDA_CHECKED uda ON uda.AUTO_KEY = cmp.CMP_AUTO_KEY
LEFT JOIN USER_DEFINED_ATTRIBUTES attr ON uda.UDA_AUTO_KEY = attr.UDA_AUTO_KEY
WHERE attr.AUTO_KEY_PREFIX = 'CMP'
此外,如果没有匹配项,请考虑删除ELSE
部分并将列值保留为NULL
,而不是放置This is blank
值。