3

我有多个提名人和民意调查的选举结果表。我需要确定每个投票中哪个被提名人​​的票数最多。

以下是表中数据的示例:

PollID NomineeID 投票
1 1 108
1 2 145
1 3 4
2 1 10
2 2 41
2 3 0

我将不胜感激任何人可以为我提供的任何建议或帮助。

4

4 回答 4

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
于 2013-10-08T20:58:38.720 回答
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
于 2013-10-08T21:06:23.600 回答
0
select Pollid, Nomineeid, Votes from Poll_table 
where Votes in ( 
    select max(Votes) from Poll_table 
    group by Pollid
);
于 2013-10-08T21:43:18.407 回答
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 位候选人在同一个投票中获得最多票数,他们都将被使用此查询拉出

于 2013-10-08T21:02:29.017 回答