[英]Order by next highest number
我有一個查詢,該查詢在多個練習中均會影響游戲玩家的排名。 如果他們的分數並列,我需要按每次練習的最高排名來解開。
例如:
total_points pos1 pos2 pos3
#1 5 1 3 1
#2 7 3 1 3
#3 7 4 1T 2
第二名是當前處於第三名的玩家,因為他在第一名與第二名並列,但他在第二名中排名第二,而第二名則是第二名。 3.(T表示並列,當玩家與另一個並列時應出現在號碼旁邊)
所以理想的結果是
total_points pos1 pos2 pos3
#1 5 1 3 1
#2 7 4 1T 2
#3 7 3 1 3
我認為排序是通過pos1排序,然后忽略其他位置?
我正在使用的后續查詢是
SELECT totals.*, scores.*, warriors.*, results.*, positions.*
FROM (SELECT results . * , SUM( points ) AS total_points
FROM final_results AS results
INNER JOIN contest_trainings AS wods ON wods.id = results.wod_id
WHERE wods.show_date < NOW( )
GROUP BY contest_id) AS totals,
final_results AS scores
INNER JOIN (
SELECT wod1.*, wod1.wod_position AS pos1, wod2.wod_position AS pos2, wod3.wod_position AS pos3
FROM final_results AS wod1
LEFT OUTER JOIN final_results AS wod2
ON wod1.contest_id = wod2.contest_id
AND wod2.wod_id > wod1.wod_id
LEFT OUTER JOIN final_results AS wod3
ON wod2.contest_id = wod3.contest_id
AND wod3.wod_id > wod2.wod_id
GROUP BY wod1.contest_id) as positions
ON positions.contest_id = scores.contest_id
INNER JOIN contest AS warriors ON scores.contest_id = warriors.id
INNER JOIN contest_results AS results ON scores.contest_id = results.contest_id
WHERE totals.contest_id = warriors.id AND results.validated = 1
AND warriors.battle = 1
AND warriors.category = 1
GROUP BY scores.contest_id
ORDER BY totals.total_points,
positions.pos1, CAST(positions.pos2 AS UNSIGNED), positions.pos3
假設您對第4位感興趣,那么我將嘗試使用以下排序:-
SELECT totals.*, scores.*, warriors.*, results.*, positions.*
FROM
(
SELECT results . * , SUM( points ) AS total_points
FROM final_results AS results
INNER JOIN contest_trainings AS wods
ON wods.id = results.wod_id
WHERE wods.show_date < NOW( )
GROUP BY contest_id
) AS totals,
CROSS JOIN final_results AS scores
INNER JOIN
(
SELECT wod1.*, wod1.wod_position AS pos1, wod2.wod_position AS pos2, wod3.wod_position AS pos3
FROM final_results AS wod1
LEFT OUTER JOIN final_results AS wod2
ON wod1.contest_id = wod2.contest_id
AND wod2.wod_id > wod1.wod_id
LEFT OUTER JOIN final_results AS wod3
ON wod2.contest_id = wod3.contest_id
AND wod3.wod_id > wod2.wod_id
GROUP BY wod1.contest_id
) as positions
ON positions.contest_id = scores.contest_id
INNER JOIN contest AS warriors ON scores.contest_id = warriors.id
INNER JOIN contest_results AS results ON scores.contest_id = results.contest_id
WHERE totals.contest_id = warriors.id AND results.validated = 1
AND warriors.battle = 1
AND warriors.category = 1
GROUP BY scores.contest_id
ORDER BY totals.total_points,
IF(positions.pos1 = 1, 1, 0) + IF(positions.pos2 = 1, 1, 0) + IF(positions.pos3 = 1, 1, 0),
IF(positions.pos1 = 2, 1, 0) + IF(positions.pos2 = 2, 1, 0) + IF(positions.pos3 = 2, 1, 0),
IF(positions.pos1 = 3, 1, 0) + IF(positions.pos2 = 3, 1, 0) + IF(positions.pos3 = 3, 1, 0),
IF(positions.pos1 = 4, 1, 0) + IF(positions.pos2 = 4, 1, 0) + IF(positions.pos3 = 4, 1, 0)
可能有可能對此進行改進,但我需要查看更多原始表。
請注意,您的查詢似乎可能有一些不確定的結果。 例如,您的第一個子查詢執行GROUP BY race_id,但返回final_results表中的所有內容。 不確定所有這些列是否都取決於race_id的單個值(但不能確定,因為我不知道final_results和race_trainings的布局方式)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.