Pivot data to return the first two country entries for each person.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |