convert columns to rows sql server -
this question has answer here:
- sql server dynamic pivot query? 6 answers
these data:
and want have result:
please note values inserted column "app", haven't got same value or same quantity, once find 2 rows , time 10 rows
you can use temp table store app values, concat them variable. using t-sql can construct dynamic pivot:
create table #vals (vals nvarchar(max)) insert #vals select distinct app [table1] declare @sql nvarchar(max) declare @vals nvarchar(max) select @vals = coalesce(@vals+', ','') + '[' + vals + ']' #vals set @sql = ' select name, '+@vals+' [table1] pivot (max([version]) app in ('+@vals+')) piv' print @sql exec (@sql)