gpt4 book ai didi

mysql - SQL Multi Order by Count 和 Criteria

转载 作者:行者123 更新时间:2023-11-29 22:07:15 26 4
gpt4 key购买 nike

我想查询MotoGP比赛结果的排名

我的sql是

SELECT `Rider`, `Team`, `Bike`,SUM(`Points`)
FROM `table_name`
WHERE `Year` = 2015 AND `Classes` = "MotoGP"
GROUP BY `Year`,`Rider`
HAVING SUM(`Points`)
ORDER BY SUM(`Points`) DESC

如果 2 个或更多车手具有相同的 SUM(“积分”),则下一个顺序是按照第一名比赛位置的编号。我将比赛位置放在“Pos”字段上。

Pos 值:1,2,3,4,5 ...

请帮忙处理第二个订单。谢谢

最佳答案

您只需更改 HAVING 子句:

ORDER BY SUM(`Points`) DESC,
SUM(RacePosition = 1) DESC

对于多个职位,我认为您需要为每个职位添加条件:

ORDER BY SUM(`Points`) DESC,
SUM(RacePosition = 1) DESC,
SUM(RacePosition = 2) DESC,
SUM(RacePosition = 3) DESC

在某些时候,也许SUM(RacePosition)会做你想要的。

关于mysql - SQL Multi Order by Count 和 Criteria,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32041779/

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