SQL查找每组的最高记录(SQL finding the top record per group)

编程入门 行业动态 更新时间:2024-10-23 07:21:45
SQL查找每组的最高记录(SQL finding the top record per group)

有点卡在查询上。 我有一个测验结果表,其中包含:

ID of the player, QuizID, Number of seconds it took to complete the quiz, Number of correct answers

我怎样才能找到每个测验的胜利者。 =具有最大正确答案数的玩家和每个QuizID的最小秒数

我尝试了很多方法,但我无法得到正确的结果。 希望有人可以帮忙。

谢谢!

Kind of stuck on a query. I have a table of quiz results which contains:

ID of the player, QuizID, Number of seconds it took to complete the quiz, Number of correct answers

How would I find the winner of every quiz. = the player with the max number of correct answers AND minimum number of seconds for every QuizID

I have tried many ways and I just can't get the correct results. Hope someone can help out.

Thanks!

最满意答案

CTE将为测验中的每个玩家计算位置。 主查询仅过滤第一个位置。

; with quiz as ( select QuizId, PlayerID, row_number() over (partition by QuizId order by count_answers desc, seconds) rownum from QuizResults ) select * from QuizResults inner join Quiz on QuizResults.QuizID = Quiz.QuizID and QuizResults.PlayerID = Quiz.PlayerID where rownum = 1

CTE will calculate place for each player in the quiz. main query filters first positions only.

; with quiz as ( select QuizId, PlayerID, row_number() over (partition by QuizId order by count_answers desc, seconds) rownum from QuizResults ) select * from QuizResults inner join Quiz on QuizResults.QuizID = Quiz.QuizID and QuizResults.PlayerID = Quiz.PlayerID where rownum = 1

更多推荐

本文发布于:2023-08-01 01:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每组   finding   SQL   group   record

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!