CREATE table #yourtable ([Id] int, [Value] varchar(6), [C...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Writing Meta

Q&A about the site for the craft of professional writing, including fiction, non-fiction, technical, scholarly, and commercial writing

CREATE table #yourtable
    ([Id] int, [Value] varchar(6), [ColumnName] varchar(13),
    [Column2] varchar(13), [Column3] varchar(13));
    
INSERT INTO #yourtable
    ([Id], [Value], [ColumnName], [Column2], [Column3])
VALUES
    (1, 'John', 'FirstName', 'test1', 'test11'),
    (2, '2.4', 'Amount', 'test2', 'test21'),
    (3, 'ZH1E4A', 'PostalCode', 'test3', 'tes31'),
    (4, 'Fork', 'LastName', 'test4', 'test41'),
    (5, '857685', 'AccountNumber', 'test5', 'test51')
;


select Column2, Column3, Firstname, Amount, PostalCode, LastName, AccountNumber
from
(
  select Column2, Column3, value, columnname
  from #yourtable
) d
pivot
(
  max(value)
  for columnname in (Firstname, Amount, PostalCode, LastName, AccountNumber)
) piv;

Enter Parameters

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