0

cn_category.category_points我需要计算每个玩家(cn_players记录)的总分( )。这是由cn_records包含当前和过时记录的 table 驱动的。我只需要总结所有类别(cn_category记录)、地图(cn_maps记录;未在下面显示)和游戏(cn_wads记录;未在下面显示)的最佳时间。此外,对于 #9、#10 和 #11 类别,将根据三个最佳分数/记录授予积分,而不仅仅是最好的。

到目前为止,我有这个查询,但它同时计算了当前和过时的记录:

SELECT *, MIN(r.record_time), SUM(cn_category.category_points)
FROM (select * from cn_records as r order by r.record_time ASC) AS r
LEFT JOIN cn_players ON r.player1_id = cn_players.player_id
LEFT JOIN cn_category ON r.category_id = cn_category.category_id
WHERE r.category_id BETWEEN 1 AND 25
GROUP BY r.player1_id
ORDER BY SUM(category_points), r.player1_id DESC

(注意:以上不是完整的查询。WHERE为简单起见,删除了一些连接和子句条件。)

类别在cn_category表中:

 category_id | category_points | ...
-------------+-----------------+-----
           1 |             1.0 | ...
           9 |             5.0 | ...
         ... |             ... | ...

玩家在cn_players表中:

 player_id | ...
-----------+-----
         1 | ...
         2 | ...
       ... | ...

“记录”在cn_records表中(有数千行):

 record_id | category_id | player1_id | record_time | ...
-----------+-------------+------------+-------------+-----
         1 |           1 |        143 |    00:00:00 | ...
         2 |           1 |        145 |    00:00:00 | ...
         3 |           1 |         41 |    00:00:00 | ...
         4 |           1 |         41 |    00:00:00 | ...
         5 |           1 |        141 |    00:00:00 | ...
         6 |           1 |         41 |    00:00:00 | ...
       ... |         ... |        ... |         ... | ...
4

1 回答 1

0

像这样的东西?您首先从 cn_records 表中选择每个类别的最大点数。然后您可以再次加入 cn_records 表以仅选择每个类别的实际记录(与最大点匹配的记录)。

之后,您可以加入播放器和类别详细信息,并根据需要进行额外的分组和过滤。困难的部分已经完成。:)

SELECT
    p.player_id,
    p.player_name,
    sum(c.category_points) AS total_points
FROM
    /* Select the max points per category */
    (SELECT
      r.category_id,
      min(r.record_time) AS best_time
    FROM
      cn_records r
    GROUP BY
      r.category_id) ar /* Actual records */
    /* Join the records table to find the records that match the max points.
       These are the only records to investigate */
    INNER JOIN cn_records r 
      ON r.category_id = ar.category_id 
      AND r.record_time = ar.best_time
    /* Inner (don't left) join player. What is a record without player */
    INNER JOIN cn_players p ON r.player1_id = p.player_id
    INNER JOIN cn_category c ON r.category_id = c.category_id
GROUP BY
    r.category_id
于 2012-09-02T09:53:50.263 回答