gpt4 book ai didi

SQL - 无法使分组查询工作

转载 作者:行者123 更新时间:2023-12-04 18:13:17 24 4
gpt4 key购买 nike

我有两张 table (MS Access)

输入

Event, UserID, ScoreEventA, 1, 50EventA, 2, 55EventB, 1, 45EventB, 2, 33

tblUser

ID, Name1     , John2     , Alex

I need to show the winning score of each event, the event, and the name of the person. Output for this example should be

Event, Name, ScoreEventA, Alex, 55EventB, John, 45

I have tried this to get the event and top score:

SELECT Max(Score), Event FROM tblInput GROUP BY Event;

但是,如果我尝试选择 ID (我只是在名称的地方使用,可以在加入表时改回名称),我被迫将其设为我不想要的聚合函数,或者将其放在 Group by 语句中,我得到类似的东西
SELECT Max(Score) AS Score, Event, ID FROM tblInput GROUP BY Event, ID;

分数、事件、ID
165 事件A 2
173 事件A 9
170 事件A 32
211 事件B 10
224 事件B 14
256 事件C 16
188 事件C 17

任何帮助表示赞赏,抱歉格式不佳。

最佳答案

像这样创建一个查询:

SELECT 
Max(Score) as topscore,
ChildEvent
FROM
T_Participation
GROUP BY ChildEvent

然后使用第一个查询构建第二个,并且您的输入表加入了事件和分数:
select
childEvent,
topscore,
StudentID
from
query1 q inner join
t_participation p on
q.childevent = p.childevent and
q.topscore = p.score

如果您有多个用户获得最高分怎么办?
与事件 A 一样,用户 3 的得分为 55?

关于SQL - 无法使分组查询工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12203000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com