From f98cc3d7c5cdc5898a5fd7bf3be70e1b8a1fccc8 Mon Sep 17 00:00:00 2001 From: Gregor Billing Date: Tue, 31 Dec 2024 12:01:22 +0900 Subject: [PATCH] Introduce second layer of best-only aggregation via ROW_NUMBER --- lib/auxiliary_data_computation.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/auxiliary_data_computation.rb b/lib/auxiliary_data_computation.rb index 3f84ce05a5..eede0ae6ce 100644 --- a/lib/auxiliary_data_computation.rb +++ b/lib/auxiliary_data_computation.rb @@ -56,13 +56,17 @@ def self.compute_rank_tables SELECT eventId, personId, countryId, continentId, min(#{field}) `value` FROM #{concise_table_name} GROUP BY personId, countryId, continentId, eventId + ), best_table AS ( + SELECT *, ROW_NUMBER() OVER (PARTITION BY eventId, personId ORDER BY value) AS really_best + FROM temp_table ) SELECT personId, eventId, `value`, RANK() OVER(PARTITION BY eventId ORDER BY `value`) AS worldRank, RANK() OVER(PARTITION BY eventId, continentId ORDER BY `value`) AS continentRank, RANK() OVER(PARTITION BY eventId, countryId ORDER BY `value`) AS countryRank - FROM personal_best + FROM best_table + WHERE really_best = 1 ORDER BY eventId, `value` SQL end