0

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

名称 ID TaskID HoursAssigned
-------------------------------------------------- --------------
约翰·史密斯 4592 A01 40
马修·琼斯 2863 A01 20
杰克亚当斯 1182 A01 100
马修·琼斯 2863 A02 50
杰克亚当斯 2863 A02 10

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

任务 ID 人员分配
-------------------------------------------------- ----------
A01 杰克·亚当斯、约翰·史密斯、马修·琼斯
A02 马修·琼斯,杰克·亚当斯

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

4

4 回答 4

4

每天都会在这里问这个问题。 这是昨天的。还有一个

https://stackoverflow.com/questions/tagged/sql+string-concatenation

于 2009-06-03T20:13:02.050 回答
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)
于 2009-06-03T20:23:15.320 回答
1

顺便说一句,将名称存储在一个字段中是个坏主意。使得查询非常困难。如果不使用通配符作为阻止数据库使用索引的第一个字符,您将如何有效地搜索“Smith”。如果名称像这样以自由形式存储,一个人可能是“John Smith”、“Smith, John”、“Smith, John”、“Smith,John”等,你不会意识到他们是同一个人。您至少应该有 first_name、middle_name、last_name、personal_Suffix,然后您可能有一个计算字段,以您希望显示的格式显示完整名称。

于 2009-06-03T20:53:38.287 回答
0

我是 sql 的新手,也是 stackoverflow 的新手,但这不起作用吗?

SELECT taskid, name FROM table GROUP BY taskid

于 2009-06-03T20:09:58.623 回答