this is my result set of sql query.
| Header | Mobile | EXE | Web | OverallOnline |
| % of Revenue contribution | 14.00 | 14.00 | 14.00 | 14.00 |
| % of your clients logging in | 14.00 | 14.00 | 14.00 | 14.00 |
| % of your clients Traded | 14.00 | 14.00 | 14.00 | 14.00 |
| National avg % of Revenue contribution | 14.00 | 14.00 | 14.00 | 14.00 |
How can i convert to
| For | % of Revenue contribution | % of your clients logging in | % of your clients Traded | National avg % of Revenue contribution |
| Mobile | ||||
| EXE | ||||
| Web | ||||
| OverallOnline |


Midhun TpRank #388 · 6.5K reputationPosted Aug 30, 2016, 9:45 PM EDT
Vinay SinghRank #604 · 4K reputationPosted Aug 31, 2016, 7:38 PM EDT
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(ColumnName)
from yourtable
group by ColumnName, id
order by id
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ' + @cols + ' from
(
select value, ColumnName
from yourtable
) x
pivot
(
max(value)
for ColumnName in (' + @cols + ')
) p '
execute(@query)