SQL 查询查找比赛中的获胜者
我有一个查询返回结果如下:
Race | Candidate | Total Votes | MaxNoOfWinners
---------------------------------------------------
1 | 1 | 5000 | 3
1 | 2 | 6700 | 3
2 | 1 | 100 | 3
2 | 2 | 200 | 3
2 | 3 | 300 | 3
2 | 4 | 400 | 3
...
我想知道是否可以编写一个查询来仅返回特定比赛的获胜者(基于 MaxNoOfWinners 和 TotalVotes)。所以对于上述我只会回来
Race | Candidate | Total Votes | MaxNoOfWinners
---------------------------------------------------
1 | 1 | 5000 | 3
1 | 2 | 6700 | 3
2 | 2 | 200 | 3
2 | 3 | 300 | 3
2 | 4 | 400 | 3
...
I have a query which returns to me results as follows:
Race | Candidate | Total Votes | MaxNoOfWinners
---------------------------------------------------
1 | 1 | 5000 | 3
1 | 2 | 6700 | 3
2 | 1 | 100 | 3
2 | 2 | 200 | 3
2 | 3 | 300 | 3
2 | 4 | 400 | 3
...
I was wondering if there was a query that could be written to return only the winners (based on the MaxNoOfWinners and TotalVotes) for a certain race. So for the above i would only get back
Race | Candidate | Total Votes | MaxNoOfWinners
---------------------------------------------------
1 | 1 | 5000 | 3
1 | 2 | 6700 | 3
2 | 2 | 200 | 3
2 | 3 | 300 | 3
2 | 4 | 400 | 3
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个解决方案...我没有测试,所以可能有拼写错误。这个想法是使用 SQL Server 的 RANK() 函数根据选票按种族给出排名,不包括那些不符合条件的人。请注意,使用 RANK() 而不是 ROW_NUMBER() 将在结果中包含平局。
Here is a solution... I did not test so there may be typos. The idea is is use the RANK() function of SQL Server to give a ranking by Race based on votes and not include those that don't meet the criteria. Note, using RANK() and not ROW_NUMBER() will include ties in the result.
这是一个完整的工作示例,假设有两个表进行竞赛和候选,
结果如下
Here's a complete working sample that assumes two tables race and candiate
With the following results