I have a query result like this :
Date User1 User2 User3 ....
----------------------------------
1/1/2000 55 78 98 ...
1/1/2001 26 33 56 ...
1/1/2002 88 67 12 ...
The number of columns is not known because it is the result of a pivot query.
I would like to change the name the columns to something that looks like this :
Date User1 (blue) User2 (green) User3(brown)
The color is an information I retrieve from another table.
How can I achieve this ?
Thanks
Edit : Here is the query.
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(C.Name)
from [History]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT [Date],' + @cols +'
from
(
select [Date], Name, Value
from [History]
) x
pivot
(
max(value)
for Name in (' + @cols + ')
) p '
execute(@query)
blue,green,browncome from? Do you want to write them manually for each user name? If so then list the columns' names manually not dynamically since, you will write the colors' names manually after all.