【问题标题】:Comma separated list from multiple sql columns [duplicate]来自多个sql列的逗号分隔列表[重复]
【发布时间】:2017-04-19 03:00:51
【问题描述】:

使用 SQL Server 2012,我有一个包含多个复选框字段的表,其中的值显然是 1 或 0。我需要提取其中任何一个值为 1 的任何这些字段。

所以:

ID      Building            Heavy         Municipal       Industry
101        1                   1              0               1

结果:

ID               Type
101              Building, Heavy, Industry

我一辈子都搞不懂语法。

【问题讨论】:

    标签: sql sql-server tsql string-aggregation


    【解决方案1】:

    假设数字列很少可以使用 IIF 2012+ 否则情况

    用于确定开始位置和结束位置的子字符串。

    select ID, 
        SUBSTRING(
        IIF(Building=1,', Building','') + 
                IIF(Heavy=1,', Heavy','') + 
                IIF(Municipal=1,', Municipal','') + 
                IIF(Industry=1,', Industry','')
                ,3,100)   -- substring to start from pos 3 and ends at position 100 depends on the desired length
               Type
    from table
    

    【讨论】:

    • 我认为必须改进连接,否则你可能会得到:BuildingHeavyMunicipalIndustry 或者我错了吗?哦,尽管想法很简单,但 +1
    • 有一个额外的逗号..我会改进这个欢呼声
    【解决方案2】:

    一种方法是, 第 1 步:取消透视表

    SELECT ID, Sub, SubVal
    INTO #t2
    FROM (SELECT * FROM #t)t
    UNPIVOT
    (
        SubVal FOR Sub IN (Building,Heavy, Muncipal, Industry)
    ) as un
    

    第 2 步:用于 FOR XML PATH,

    SELECT DISTINCT ID,
        STUFF((
            SELECT ' , ' + t2.Sub  
            FROM #t2 t2
            WHERE SubVal = 1
            FOR XML PATH('')
        ), 1, 2, '')   AS Type
    FROM #t2 ct
    

    【讨论】:

      【解决方案3】:

      您也可以像这样使用CASE 语句:

      create table test (
          id int,
          building int,
          heavy int,
          municipal int,
          industry int
      );
      insert into test values (101, 1, 1, 0, 1);
      
      with data as (
          select id,
              case when building = 1 then 'Building,' else '' end +
              case when heavy = 1 then 'Heavy,' else '' end +
              case when municipal = 1 then 'Municipal,' else '' end +
              case when industry = 1 then 'Industry,' else '' end as fld
          from test
      )
      select id, left(fld, len(fld)-1) as fld from data;
      

      示例:http://rextester.com/CKGES46149

      结果:

      id  fld
      101 Building,Heavy,Industry
      

      如果逗号后需要一个空格,请稍加修改,如下所示:

      with data as (
          select id,
              rtrim(
              case when building = 1 then 'Building, ' else '' end +
              case when heavy = 1 then 'Heavy, ' else '' end +
              case when municipal = 1 then 'Municipal, ' else '' end +
              case when industry = 1 then 'Industry, ' else '' end
              ) as fld
          from test
      )
      select id, left(fld, len(fld)-1) as fld from data;
      

      结果:

      id  fld
      101 Building, Heavy, Industry
      

      示例:http://rextester.com/OJNEQ98420

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-19
        • 2012-10-14
        相关资源
        最近更新 更多