From acab69b65c643babdb8f0630fa854bbc7164079d Mon Sep 17 00:00:00 2001 From: OpenSquared Date: Thu, 25 Jun 2026 21:53:14 +0200 Subject: [PATCH] fix: always GROUP BY me.id in market_events list query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The LEFT JOIN on instrument_impacts (added for the evaluated subquery) generates N rows per event when the event has N impacts. GROUP BY was only applied when the instrument filter was active — now it's always on. Fixes: total=2 but list shows 6+ duplicates. Co-Authored-By: Claude Sonnet 4.6 --- backend/routers/market_events.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/routers/market_events.py b/backend/routers/market_events.py index fc07fef..d19a1f7 100644 --- a/backend/routers/market_events.py +++ b/backend/routers/market_events.py @@ -168,8 +168,9 @@ def list_events( sort_col = "me.impact_score" order_sql = f"ORDER BY {sort_col} {'DESC' if sort_dir == 'desc' else 'ASC'}" - # When joining with instrument_impacts an event can appear multiple times — use GROUP BY - group_sql = "GROUP BY me.id" if instrument else "" + # Always GROUP BY — the LEFT JOIN on instrument_impacts creates duplicate rows + # when an event has multiple impacts (one row per impact without GROUP BY) + group_sql = "GROUP BY me.id" # Extra columns: evaluated flag + optional instrument score evaluated_col = (