Data Expose

0

Please login or register to vote for this query.

(click on this box to dismiss)

Reverse Engineering Meta

Q&A about the site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation

CREATE table #yourtable
    ([Id] int, [Value] varchar(10), [ColumnName] varchar(13),[RowId] varchar(13))
;
    
INSERT INTO #yourtable
    ([Id], [Value], [ColumnName],[RowId])
VALUES
    (1, 'John', 'FirstName','E01'),
    (2, '2.4', 'Amount','E01'),
    (3, 'ZH1E4A', 'PostalCode','E01'),
    (4, 'Fork', 'LastName','E01'),
    (5, '857685', 'AccountNumber','E01'),
    (6, 'E01', 'RowId','E01')
  
;


DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(ColumnName) 
                    from #yourtable
                    group by ColumnName, RowId
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select value, ColumnName
                from #yourtable
            ) x
            pivot 
            (
                max(value)
                for ColumnName in (' + @cols + N')
            ) p '

exec sp_executesql @query;

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results