1

表结构:

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(积分)

4

2 回答 2

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 ;   
于 2012-07-11T09:50:12.803 回答
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
于 2012-07-11T09:50:32.100 回答