Sql server 我在这句话中的第二个内部连接有什么问题
此查询可用于:Sql server 我在这句话中的第二个内部连接有什么问题,sql-server,inner-join,Sql Server,Inner Join,此查询可用于: select rc.[race number], 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 =
select rc.[race number],
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 = 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
from
(
select rc.[race number],
rc.[candidate num],
rc.[Votes],
c.[party],
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]
) rc
group by rc.[race number]
现在,我需要加入第三个名为RACE的表中的信息。我尝试了以下查询,但在第二个内部联接之前的from语句中出错:
select rc.[race number]
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 = 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
from
(
select rc.[race number],
rc.[candidate num],
rc.[Votes],
c.[party],
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]
from dbo.[RACE] r
inner join dbo.[RACE CANDIDATES] on rc.[race number] = r.[race number]
) rc
group by rc.[race number]
我不确定我在这里做错了什么。这可能吗?我知道我已经在这个问题上发表了很多文章,但我只是想把它做好。提前感谢您的宝贵帮助。更改查询如下
select rc.[race number]
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 = 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
from
(
select rc.[race number],
rc.[candidate num],
rc.[Votes],
c.[party],
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]
您有两个
FROM
关键字。因此,将dbo.[RACE]r的替换为内部连接dbo.[RACE]r ON r.[RACE number]=rc.[RACE number]
关键字“group”附近的语法不正确。我还试着从第三个表中查询一个元素,我想我得到了。非常感谢你!