将多行连接成一行

时间:2013-04-29 21:25:45

标签: sql sql-server tsql

我刚刚在SQL Server上学习SQL。我需要将多行连接成一列。我找了一些例子,但找不到一个我可以用来满足我需要的例子。

Country     ProjectTA   Complexity TID  Sites   Inits   Name
United States A8022 Obesity Low  4692   69  JT  AD
United States A8022 Obesity Low  4692   69  jpni    CBM Budget
United States A8022 Obesity Low  4692   69  PIHR    AD
United States A8022 Obesity Low  4692   69  jpni    CBM Budget
United States A8022 Obesity Low  4692   69  hale    ePublishing Group
United States S8033 CNS Medium   5423   69  ShyP    CBM Payment
United States S8033 CNS Medium   5423   69  dedu    ePublishing Group
United States S8033 CNS Low  5423   69  AHrp    ePublishing Group

我希望按名称连接行,并使用','

列出Inits
United States A8022 Obesity Low 4692    69 JT,PIHR  AD
United States A8022 Obesity Low 4692    69 jpni, PIHR   CBM Budget
United States A8022 Obesity Low 4692    69 hale          ePublishing Group
United States S8033 CNS Medium  5423    69 ShyP          CBM Payment
United States S8033 CNS Medium  5423    69 dedu, Ahrp   ePublishing Group

任何帮助将不胜感激。非常感谢。

2 个答案:

答案 0 :(得分:6)

不幸的是,SQL Server没有简单的函数来生成以逗号分隔的列表,您必须实现FOR XML PATH才能获得列表。

有几种方法可以使用STUFFFOR XML PATH

select distinct t1.country,
  t1.ProjectTA, 
  t1.Complexity,
  t1.TID,
  t1.Sites,
  STUFF(
         (SELECT ', ' + t2.Inits
          FROM yt t2
          where t1.Country = t2.Country
            and t1.ProjectTA = t2.ProjectTA
            and t1.TID = t2.TID
            and t1.Sites = t2.Sites
            and t1.name = t2.name
          FOR XML PATH (''))
          , 1, 1, '')  AS inits,
  t1.name
from yt t1;

请参阅SQL Fiddle with demo

或者您可以使用CROSS APPLYFOR XML PATH

select distinct t1.country,
  t1.ProjectTA, 
  t1.Complexity,
  t1.TID,
  t1.Sites, 
  left(t2.inits, len(t2.inits)-1) inits,
  t1.name
from yt t1
cross apply
(
  select t2.Inits + ', '
  from yt t2
  where t1.Country = t2.Country
    and t1.ProjectTA = t2.ProjectTA
    and t1.TID = t2.TID
    and t1.Sites = t2.Sites
    and t1.name = t2.name
  FOR XML PATH('')
) t2 (inits);

SQL Fiddle with Demo。这两者都会产生结果:

|       COUNTRY | PROJECTTA |  COMPLEXITY |  TID | SITES |      INITS |              NAME |
-------------------------------------------------------------------------------------------
| United States |     A8022 | Obesity Low | 4692 |    69 |       hale | ePublishing Group |
| United States |     A8022 | Obesity Low | 4692 |    69 | jpni, jpni |        CBM Budget |
| United States |     A8022 | Obesity Low | 4692 |    69 |   JT, PIHR |                AD |
| United States |     S8033 |     CNS Low | 5423 |    69 | dedu, AHrp | ePublishing Group |
| United States |     S8033 |  CNS Medium | 5423 |    69 | dedu, AHrp | ePublishing Group |
| United States |     S8033 |  CNS Medium | 5423 |    69 |       ShyP |       CBM Payment |

答案 1 :(得分:3)

要在SQL Server中执行此操作,您需要将字符串连接在一起作为aggregatino函数。不幸的是,SQL Server没有group_concat()listagg()或一些相关的功能。这是SQL Server中的方法:

select Country, ProjectTA, Complexity, TID,
       stuff((select ', '+Inits
              from t t2
              where t2.Name = t.Name and
                    t2.country = t.country and
                    t2.ProjectTA = t.ProjectTA and
                    t2.Tid = t.Tid
              for xml path(''), type
             ).value('.','nvarchar(max)'), 1, 2, ''
            ) as InitsList,
       Name
from t
group by country, ProjectTA, Complexity, TID, name;

看起来您的分组不仅仅是名称,因此我将inits以外的所有字段都包括在内。