这是我的疑问:
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
from
(
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]
) rc
group by rc.[race number]
select rc.[race number] AS RaceNumber,
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 LoserSelected,
max(case when seqnum = 5 then title1 end) as title1,
max(case when seqnum = 5 then [precinct percent] end) as PrecintPercent,
max(case when seqnum = 5 then [candidate num] end) as Winner,
max(case when seqnum = 5 then Votes end) as WinningVotes,
max(case when seqnum = 5 then party end) as WinningParty,
max(case when seqnum = 5 then [first name]+[last name] end) as Winner1,
max(case when seqnum = 5 then CAST(winner AS tinyint) end) as WinnerSelected,
max(case when seqnum = 6 then [candidate num] end) as Loser,
max(case when seqnum = 6 then Votes end) as LosingVotes,
max(case when seqnum = 6 then party end) as LosingParty,
max(case when seqnum = 6 then [first name]+[last name] end) as Loser2,
max(case when seqnum = 6 then CAST(winner AS tinyint) end) as LoserSelected
from
(
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]
) rc
group by rc.[race number]
这导致2个查询窗口。我想知道我是否可以将它们都放在一个查询中,同时保持它们分开。我相信如果我可以在结束之前在select语句中拆分查询,我可以在使用FOR XML子句之前分离结果。我正在尝试各种不同的方法来实现这一目标。
谢谢
感谢。
答案 0 :(得分:0)
很难说这是否会运行但是请尝试此查询,这将在您的第一个选择中创建缺少的列。如果您发布了表格结构,我们可以提供更多帮助。
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,
'' AS title1,
'' AS PrecintPercent,
'' AS Winner,
'' AS WinningVotes,
'' AS WinningParty,
'' AS Winner1,
'' AS WinnerSelected,
'' AS Loser,
'' AS LosingVotes,
'' AS LosingParty,
'' AS Loser2,
'' AS LoserSelected
FROM
(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]) rc
GROUP BY rc.[race number]
UNION
SELECT rc.[race number] AS RaceNumber,
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 LoserSelected,
max(CASE WHEN seqnum = 5 THEN title1 END) AS title1,
max(CASE WHEN seqnum = 5 THEN [precinct percent] END) AS PrecintPercent,
max(CASE WHEN seqnum = 5 THEN [candidate num] END) AS Winner,
max(CASE WHEN seqnum = 5 THEN Votes END) AS WinningVotes,
max(CASE WHEN seqnum = 5 THEN party END) AS WinningParty,
max(CASE WHEN seqnum = 5 THEN [first name]+[last name] END) AS Winner1,
max(CASE WHEN seqnum = 5 THEN CAST(winner AS tinyint) END) AS WinnerSelected,
max(CASE WHEN seqnum = 6 THEN [candidate num] END) AS Loser,
max(CASE WHEN seqnum = 6 THEN Votes END) AS LosingVotes,
max(CASE WHEN seqnum = 6 THEN party END) AS LosingParty,
max(CASE WHEN seqnum = 6 THEN [first name]+[last name] END) AS Loser2,
max(CASE WHEN seqnum = 6 THEN CAST(winner AS tinyint) END) AS LoserSelected
FROM
(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]) rc
GROUP BY rc.[race number]