Here's my query:select rc.[race number] AS RaceNumber, max(case when seqnum = 1 then title1 end) as title1, max(case when seqnum = 1 then [precinct percent] end) as PrecintPercent, max(case when seqnum = 1 then [candidate num] end) as Winner, max(case when seqnum = 1 then Votes end) as WinningVotes, max(case when seqnum = 1 then party end) as WinningParty, max(case when seqnum = 1 then leader end) as Winner1, max(case when seqnum = 1 then CAST(winner AS tinyint) end) as WinnerSelected, max(case when seqnum = 1 then [leader percent] end) as WinnerPercent, max(case when seqnum = 2 then [candidate num] end) as Loser, max(case when seqnum = 2 then Votes end) as LosingVotes, max(case when seqnum = 2 then party end) as LosingParty, max(case when seqnum = 2 then leader2 end) as Loser2, max(case when seqnum = 2 then [leader2 percent] end) as LoserPercent, max(case when seqnum = 2 then CAST(winner AS tinyint) end) as LoserSelected, max(case when seqnum = 3 then title1 end) as title1, max(case when seqnum = 3 then [precinct percent] end) as PrecintPercent, max(case when seqnum = 3 then [candidate num] end) as Winner, max(case when seqnum = 3 then Votes end) as WinningVotes, max(case when seqnum = 3 then party end) as WinningParty, max(case when seqnum = 3 then [first name]+[last name] end) as Winner1, max(case when seqnum = 3 then CAST(winner AS tinyint) end) as WinnerSelected, max(case when seqnum = 4 then [candidate num] end) as Loser, max(case when seqnum = 4 then Votes end) as LosingVotes, max(case when seqnum = 4 then party end) as LosingParty, max(case when seqnum = 4 then [first name]+[last name] end) as Loser2, max(case when seqnum = 4 then CAST(winner AS tinyint) end) as LoserSelectedfrom (select r.title1, r.[precinct percent], rc.[race number], rc.[candidate num], rc.[Votes], rc.[winner], c.[party], r.[leader], r.[leader percent], r.[leader2], r.[leader2 percent], c.[first name], c.[last name], row_number() over (partition by rc.[race number] order by votes desc) as seqnum from dbo.[RACE CANDIDATES] rc inner join dbo.[CANDIDATE] c on rc.[candidate num] = c.[candidate number] inner join dbo.[RACE] r on rc.[race number] = r.[race number] ) rcgroup by rc.[race number]FOR XML PATH ('ELECTION'), ROOT('root')
This is the output: