本文介绍了如何在一个查询中连接一列中的多个行+内连接的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询,结果如下:
查询:
I have a query with the following result:
query:
SELECT Tasks.TaskId, Comments.Comment, comments.timespent
FROM comments
INNER JOIN tasks ON comments.entityid = tasks.taskid
WHERE ( comments.entity = 1 )
GROUP BY Tasks.TaskId, Comments.Comment, comments.timespent
结果:
任务ID评论TimeSpent
_______ ________ _________
111754 C1 4
111754 C2 1
111754 C3 79
请告诉我如何编写查询以获得结果低点:
任务ID评论TimeSpent
_______ ___________ _________
111754,C1,C2,C3 84
提前谢谢。
result:
TaskID Comment TimeSpent
_______ ________ _________
111754 C1 4
111754 C2 1
111754 C3 79
Please tell me how should I write my query to get the result as follows:
TaskID Comment TimeSpent
_______ ___________ _________
111754 ,C1,C2,C384
Thanks in advance.
推荐答案
SELECT Tasks.TaskId, SUM(comments.timespent),
stuff( (SELECT ','+Comment
FROM comments p2
WHERE p2.entityid = comments.entityid
ORDER BY Comment
FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
,1,1,'')
AS Comments
FROM comments
INNER JOIN tasks ON comments.entityid = tasks.taskid
WHERE ( comments.entity = 1 )
GROUP BY Tasks.TaskId
这篇关于如何在一个查询中连接一列中的多个行+内连接的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!