I have a table with the following results (from an SQL Query)
ID | Type | Comment |
---|---|---|
12345 | Manager Comment | This is a Manager comment |
12345 | HR Comment | This is a HR comment |
12345 | HR Comment | This is another HR comment |
54321 | Manager Comment | This is a Manager comment |
54321 | Manager Comment | This is another Manager comment |
54321 | Manager Comment | This is another Manager comment aswell |
I'm tryring to get the output to look like this:
ID | Manager Comment 1 | Manager Comment 2 | Manager Comment 3 | HR Comment 1 | HR Comment 2 |
---|---|---|---|---|---|
12345 | This is a HR comment | This is a HR comment | This is another HR Comment | ||
54321 | This is a Manager comment | This is another Manager comment | This is another Manager comment aswell |
This number of comments can be variable between 0 and 5.
I've tried the following but it only works on the newest comments in each section:
SELECT *
FROM
(
SELECT
A.[SHIFTASSIGNID] as 'ID'
,c.[COMMENTTEXT] as 'Comment Type'
,b.COMNTNOTETXT as 'Comment'
FROM [tkcsdb].[dbo].[SHFTASGNCOMNTMM] A
join [tkcsdb].[dbo].[COMNTNOTEDTL] B on
a.[COMNTNOTEID] = b.[COMNTNOTEID]
join [tkcsdb].[dbo].[COMMENTS] C
on c.commentID = a.commentID
where A.SHIFTASSIGNID = 7354246
) AS SourceTable PIVOT(max([Comment]) FOR [Type] IN([HR Notes],[Manager Notes], [Kommentar])) AS PivotTable;
Obviously it won't accomodate multipe comments, I've spent about 3 hours on this at the minute so any help would be much appreciated.
Thanks Chris