Skip to content

Instantly share code, notes, and snippets.

@jrgcubano
Forked from stevenh77/sql-pivot.sql
Created June 17, 2016 13:44
Show Gist options
  • Save jrgcubano/b52a91382d48c394c50e4ac3248cd3d7 to your computer and use it in GitHub Desktop.
Save jrgcubano/b52a91382d48c394c50e4ac3248cd3d7 to your computer and use it in GitHub Desktop.
select * from Source_Table;
select * from
( select
person,
country,
'country' + cast(rank()
over (partition by person order by id)
as varchar(10))
as countryrank
from dbo.Source_Table) as rankedSource
pivot
( max (country) for countryrank in (country1, country2)) as pivottable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment