From b7ced94f0fbef600ad8eacaa843b302301bb6bdb Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 15:07:01 +0200 Subject: [PATCH] fix: prevent repetition of template IDs in `template_usage_by_day` (#8693) --- coderd/database/queries.sql.go | 13 +++++++++---- coderd/database/queries/insights.sql | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 841b1ef1dc..120edc4eb9 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1400,11 +1400,16 @@ WITH d AS ( GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( SELECT - from_, - array_agg(DISTINCT template_id) AS ids - FROM usage_by_day, unnest(template_ids) template_id + template_usage_by_day.from_, + array_agg(template_id) AS ids + FROM ( + SELECT DISTINCT + from_, + unnest(template_ids) AS template_id + FROM usage_by_day + ) AS template_usage_by_day WHERE template_id IS NOT NULL - GROUP BY from_, template_ids + GROUP BY template_usage_by_day.from_ ) SELECT diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index e7e5f34b80..e611ab209f 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -94,11 +94,16 @@ WITH d AS ( GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( SELECT - from_, - array_agg(DISTINCT template_id) AS ids - FROM usage_by_day, unnest(template_ids) template_id + template_usage_by_day.from_, + array_agg(template_id) AS ids + FROM ( + SELECT DISTINCT + from_, + unnest(template_ids) AS template_id + FROM usage_by_day + ) AS template_usage_by_day WHERE template_id IS NOT NULL - GROUP BY from_, template_ids + GROUP BY template_usage_by_day.from_ ) SELECT