首页 > 解决方案 > 我无法选择“案例”字段

问题描述

我无法选择一个字段并需要帮助。

WHEN dpav.isprimary = 1
THEN ct.accountnum
ELSE concat (ct.accountnum,'_',dpav.zipcode) 
END 'Customeraccount'

FROM子句之前,我想再次选择 case 子句(Customeraccount)

case 
when DeliveryAddresses <> '' or ct.INVOICEACCOUNT = '' then 'Yes' 
when DeliveryAddresses <> '' or ct.INVOICEACCOUNT = '' 
 and Customeraccount LIKE '%[_]%' then 'No'
else 'No' 
end 'KeyAccount'

我尝试使用引号('Customeraccount'),但它不起作用。

标签: sqlcase

解决方案


您不能在定义它的同一个选择中使用列别名。

您仍然可以如下更新您的条件,它将为您提供所需的输出。

case 
when (DeliveryAddresses <> '' or ct.INVOICEACCOUNT = '') and dpav.isprimary = 1 then 'Yes'
when DeliveryAddresses <> '' or ct.INVOICEACCOUNT = '' then 'No'
else 'No' 
end 'KeyAccount'

即使省略第二个 where claus 也可以正常工作。

case 
when (DeliveryAddresses <> '' or ct.INVOICEACCOUNT = '') and dpav.isprimary = 1 then 'Yes'
else 'No' 
end 'KeyAccount'

推荐阅读