I'm currently working on a query in T-SQL on SQL Server 2012. I gather several Titles from a table via a join from another table. My tables look like this:
Table Project:
ProjectId |
-----------+
1 |
2 |
3 |
Table ProjectResources
ResourceId | Label | ProjectId
-----------+---------------+-----------
2 | Resource X | 1
3 | Resource Y | 1
4 | Resource Z | 2
5 | Resource A | 2
6 | Resource X | 3
the desired output in my temporary table should look like this:
ProjectId | Label |
-----------+--------------------------+
1 | Resource X, Resource Y |
2 | Resource Z, Resource A |
3 | Resource X |
I want to concat all Labels with the same ProjectId. My current query looks like this:
CREATE TABLE #tmpProjects
(
ProjectId INT
, Label VARCHAR(1000)
)
SELECT [P].ProjectId, [PR].Label
INTO #tmpProjects
FROM [MySchema].[ProjectResources] [PR]
INNER JOIN dbo.Project [P] ON [P].ProjectId= [PR].ProjectId
Unfortunatly my query creates a new row for each Resouce Label. Do you know how to create a new temp table with "concatinated" merged rows for each resource holding the same ProjectId?
Thanks a lot!