CREATE table #yourtable ([Id] int, D int, [Value] varchar...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Computer Science Educators

Q&A for those involved in the field of teaching Computer Science

CREATE table #yourtable
    ([Id] int, D int, [Value] varchar(6), [ColumnName] varchar(13))
;
    
INSERT INTO #yourtable
    ([Id], D, [Value], [ColumnName])
VALUES
    (1, 1, 'John', 'FirstName'),
    (2, 1, '2.4', 'Amount'),
    (3, 1, 'ZH1E4A', 'PostalCode'),
    (4, 1, 'Fork', 'LastName'),
    (5, 1, '857685', 'AccountNumber'),
    (6, 2, 'John1', 'FirstName'),
    (7, 2, '2.41', 'Amount'),
    (8, 2, 'ZH1E41', 'PostalCode'),
    (9, 2, 'Fork1', 'LastName'),
    (10, 2, '857681', 'AccountNumber')
;


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

Enter Parameters

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