表格结构:
country
season
points
当前查询:
SELECT SUM(points) AS total, country
FROM table
WHERE season >= X
GROUP BY country
ORDER BY total desc
这给了我一个很好的清单,按照给定国家收集的总积分排序。但是,如果一个国家与另一个国家并列,我想在最近一个季节按他们的分数排序,这可能在同一个查询中吗?如果是这样,怎么样? (记住它目前分组)
行的示例 丹麦(国家),1(赛季),10(分) 丹麦(国家),2(赛季),5(分) 瑞典(国家),1(赛季),5(分) 瑞典(国家),2(赛季),10(分)
答案 0 :(得分:1)
SELECT grp.total, grp.country
FROM
( SELECT SUM(points) AS total, country, MAX(season) AS max_season
FROM table
WHERE season >= X
GROUP BY country
) AS grp
LEFT JOIN
table AS t
ON t.country = grp.country
AND t.season = LATEST_SEASON
ORDER BY
grp.total DESC
, t.points DESC ;
答案 1 :(得分:1)
也许这会做你的伎俩:
SELECT SUM(points) AS total, country
FROM table
WHERE season >= X
GROUP BY country
ORDER BY total DESC, (SELECT t2.points FROM table t2 WHERE table.country=t2.country ORDER BY t2.season LIMIT 1) DESC