我有多个候选人和民意调查的选举结果表。我需要确定哪个被提名者每次投票的票数最多。
以下是表格中的数据样本:
PollID NomineeID Votes 1 1 108 1 2 145 1 3 4 2 1 10 2 2 41 2 3 0
我很感激任何人可以提供给我的任何建议或帮助。
答案 0 :(得分:3)
这将匹配最高,并且还会带回关系。
select sd.*
from sampleData sd
inner join (
select PollID, max(votes) as MaxVotes
from sampleData
group by PollID
) x on
sd.PollID = x.PollID and
sd.Votes = x.MaxVotes
答案 1 :(得分:2)
SELECT
t.NomineeID,
t.PollID
FROM
( SELECT
NomineeID,
PollID,
RANK() OVER (PARTITION BY i.PollID ORDER BY i.Votes DESC) AS Rank
FROM SampleData i) t
WHERE
t.Rank = 1
答案 2 :(得分:0)
SELECT PollID, NomineeID, Votes
FROM
table AS ABB2
JOIN
(SELECT PollID, MAX(Votes) AS most_votes
FROM table) AS ABB1 ON ABB1.PollID = ABB2.PollID AND ABB1.most_votes = ABB2.Votes
请注意,如果您有2名同一个民意调查的票数相同的被提名者,他们都将使用此查询提取
答案 3 :(得分:0)
select Pollid, Nomineeid, Votes from Poll_table
where Votes in (
select max(Votes) from Poll_table
group by Pollid
);