我正在尝试将此表中的数据添加到现有查询中:
我想创建一个名为“Assertions”的列,并且只想提取值为“Y”的列名称。例如,对于 Control_ID 13009(第一行),新的断言列将显示:
Control_ID Assertions
13009 'Rights_Obligation, Valuation_Allocation, Existence_Occurence'
我尝试使用如下所示的 case when 语句:
case when ca.PRESENTATION_DISCLOSURE = 'Y' then 'Presentation_Disclosure'
when ca.RIGHTS_OBLIGATION = 'Y' then 'Rights_Obligation'
when ca.VALUATION_ALLOCATION = 'Y' then 'Valuation_Allocation'
when ca.COMPLETENESS = 'Y' then 'Completeness'
when ca.EXISTENCE_OCCURRENCE = 'Y' then 'Existence_Occurence'
end as Assertions
此查询不起作用,因为它只是将带有“Y”值的第一个断言放入列中。如何修改查询以使其包含值为“Y”的所有断言?
最佳答案
您可以按如下方式使用CONCAT
:
TRIM( ',' FROM
CONCAT(CASE when ca.PRESENTATION_DISCLOSURE = 'Y' then 'Presentation_Disclosure' END,
CASE when ca.RIGHTS_OBLIGATION = 'Y' then ',Rights_Obligation' END,
CASE when ca.VALUATION_ALLOCATION = 'Y' then ',Valuation_Allocation' END,
CASE when ca.COMPLETENESS = 'Y' then ',Completeness' END,
CASE when ca.EXISTENCE_OCCURRENCE = 'Y' then ',Existence_Occurence' END
)
) as Assertions
在 2014 年,您可以使用以下查询,因为 TRIM 在 2014 年不可用。
Stuff(
CONCAT(CASE when ca.PRESENTATION_DISCLOSURE = 'Y' then 'Presentation_Disclosure' END,
CASE when ca.RIGHTS_OBLIGATION = 'Y' then ',Rights_Obligation' END,
CASE when ca.VALUATION_ALLOCATION = 'Y' then ',Valuation_Allocation' END,
CASE when ca.COMPLETENESS = 'Y' then ',Completeness' END,
CASE when ca.EXISTENCE_OCCURRENCE = 'Y' then ',Existence_Occurence' END
)
1, 1, '') as Assertions
关于sql - 如何使用 case 语句将多个值放入一列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65607705/