在SQL Server中将多个字段合并到一个文本字段中

时间:2009-06-03 20:03:19

标签: sql sql-server tsql string-concatenation

所以我有一个看起来像这样的表:

Name                 ID           TaskID           HoursAssigned
----------------------------------------------------------------
John Smith           4592         A01              40
Matthew Jones        2863         A01              20
Jake Adams           1182         A01              100
Matthew Jones        2863         A02              50
Jake Adams           2863         A02              10

我希望返回一个如下所示的数据集:

TaskID               PeopleAssigned
------------------------------------------------------------
A01                  Jake Adams, John Smith, Matthew Jones
A02                  Matthew Jones, Jake Adams

这里的问题是我不知道有多少人被分配到给定的任务。任何建议都会很棒!

4 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

尝试这个:

declare @table table (name varchar(30), ID int, TaskID char(3), HoursAssigned int)

insert into @table values ('John Smith'   ,4592 ,'A01'  ,40)
insert into @table values ('Matthew Jones',2863 ,'A01'  ,20)
insert into @table values ('Jake Adams'   ,1182 ,'A01'  ,100)
insert into @table values ('Matthew Jones',2863 ,'A02'  ,50)
insert into @table values ('Jake Adams'   ,2863 ,'A02'  ,10)

--formatted so you can see what is happening
SELECT DISTINCT
    t1.TaskID
       ,SUBSTRING(
                  replace(
                          replace(
                                  (SELECT
                                       t2.Name
                                       FROM @Table AS t2
                                       WHERE t1.TaskID=t2.TaskID
                                       ORDER BY t2.Name
                                       FOR XML PATH(''))
                                 ,'</NAME>','')
                         ,'<NAME>',', ')
                 ,3,2000)  AS PeopleAssigned
    FROM @table AS t1

基于昨天的答案!

这是输出:

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
TaskID PeopleAssigned
------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A01    Jake Adams, John Smith, Matthew Jones
A02    Jake Adams, Matthew Jones

(2 row(s) affected)

答案 2 :(得分:1)

和BTW,将名称存储在一个字段中是个坏主意。使查询变得非常困难。如果不使用通配符作为阻止数据库使用索引的第一个字符,您将如何有效地搜索“Smith”。如果名字像这样存放自由形式,那么一个人可能是“约翰史密斯”,“史密斯,约翰”,“史密斯,约翰”,“史密斯,约翰”等等,你不会意识到他们是同一个人。您应该在最小值具有first_name,middle_name,last_name,personal_Suffix,然后您可以使用一个计算字段,以您希望显示的格式显示完整名称。

答案 3 :(得分:0)

我是sql的新手,也是stackoverflow的新手,但这不会有用吗?

SELECT taskid,name FROM table GROUP BY taskid