CREATE table #yourtable ([Id] int, [Year] varchar(6), [Co...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Raspberry Pi Meta

Q&A about the site for users and developers of hardware and software for Raspberry Pi

CREATE table #yourtable
    ([Id] int, [Year] varchar(6), [ColumnName] varchar(13))
;
    
INSERT INTO #yourtable
    ([Id], [Year], [ColumnName])
VALUES
    (1, '2012', 'FirstName'),
    (2, '2012', 'Amount'),
    (3, '2010', 'PostalCode'),
    (4, '2011', 'LastName'),
    (5, '2012', 'AccountNumber'),
    (6, '2010', 'AccountNumber'),
    (7, '2009', 'AccountNumber')
    
;


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

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results