Concat多线一体

时间:2012-12-13 13:12:22

标签: sql sql-server

  

可能重复:
  Concatenate many rows into a single text string?
  Simulating group_concat MySQL function in Microsoft SQL Server 2005?

我正在使用MS Sql Server 2008, 我有下表

----------------
Uid | Alias |
--------------- |
1 | Pierre |
1 | Patrick |
1 | Jean |
2 | Alice |
2 | Diana |

我希望以这种方式显示它:

------------------------|
Uid | Alias |
------------------------|
1 | Pierre Patrick Jean|
2 | Alice Diana |

任何想法都会受到欢迎。

1 个答案:

答案 0 :(得分:1)

请尝试:

select b.Uid,
    (select a.Alias +' ' from TableName a WHERE a.Uid=b.Uid group by a.Alias FOR XML PATH(''))as Names
from TableName b
group by b.Uid