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
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,C3 84
Thanks in advance.
You can do with CROSS APPLY with XML Path such as:
You should look into
FOR XML PATH
.Ok, this is a bit more complicated but it doesn't use xml, and it can be used with other databases than sql server:
Another approach that works only for sql server would be to build an aggregate CLR function that concatenates the values: http://msdn.microsoft.com/en-us/library/91e6taax%28v=vs.90%29.aspx.
If you came across this article but you use oracle, you have the option to use the query above or define a custom aggregate function in pl/sql (http://docs.oracle.com/cd/B28359_01/appdev.111/b28425/aggr_functions.htm).
Here's the working SQL Fiddle: http://sqlfiddle.com/#!3/3597a/3
Here's the actual working SQL.
Create Table and Populate Data
Execute SQL
View Results.