declare @collist nvarchar(max)
SET @collist = stuff((select distinct ‘,’ + QUOTENAME(name)
FROM #t – your table here
FOR XML PATH(‘’), TYPE
).value(‘.’, ‘NVARCHAr(MAX)’)
,1,1,’‘)
declare @q nvarchar(max)set @q = 'select * from ( select rn, name, Value from ( select *, row_number() over (partition by name order by RecID desc) as rn from #t -- your table here ) as x) as sourcepivot ( max(Value) for name in (' + @collist + ')) as pvt'exec (@q)


