I have two tables that i like to combine together, the first are just an normal table. But the second the structure i having the problem with.
I need to convert the rows in the column [Name] to columns. The two problem im having is that the picture only shows data from one id, and i need to convert distinct list of columns and then display the [value] column as rows. Second issue is to do all this while still joining the main table together with the table in the picture. I know that pivot exists and i have tried it. But can't get it to work, just with the main table.
Does anybody have an idea to have to solve this issue??
Here is my unsuccessful attempt.
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME([Name])
from [dbo].[TfsItem]
group by [Name]
order by [Name]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SELECT @cols
set @query = N'SELECT ' + @cols + N' from
(
select value, [Name]
from [dbo].[TfsItem]
) x
pivot
(
max(value)
for [Name] in (' + @cols + N')
) p '
exec sp_executesql @query;