Sql 如何使用case语句将多个值放入一列中?

Sql 如何使用case语句将多个值放入一列中?,sql,sql-server,Sql,Sql Server,我正在尝试将此表中的数据添加到现有查询: 我想创建一个名为“Assertions”的列,只想在值为“Y”的列名中输入。例如,对于Control_ID 13009(第一行),新的断言列将显示: Control_ID Assertions 13009 'Rights_Obligation, Valuation_Allocation, Existence_Occurence' 我尝试使用一个case-when语句,如下所示: case when ca.PRESENTATION_

我正在尝试将此表中的数据添加到现有查询:

我想创建一个名为“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 Server 2017开始,您可以使用
CONCAT\u WS()

对于早期版本,
CONCAT()
是一个选项:

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, 2, ''
) AS Assertions

什么是SQL Server版本?我使用的是SQL Server management studio 2014您也使用Oracle吗?如果没有,请删除该标记。
TRIM()
可从SQL Server 2017获得。OP使用SQL Server Management Studio 2014,虽然它是一个cleint工具,但我猜SQL Server版本也是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, 2, ''
) AS Assertions