From 1473961ef0bcab003fe924d6ea41d2a685ec77af Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Thu, 1 Nov 2018 17:51:29 +0530 Subject: [PATCH] Fix next-campaigns query to return campaign_ids with 0 counts as well when there were 0 subscribers in target_lists. This would result in started_at never being updated in the update sub query. --- queries.sql | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/queries.sql b/queries.sql index a87a813..acf8996 100644 --- a/queries.sql +++ b/queries.sql @@ -198,12 +198,12 @@ WITH camps AS ( ), counts AS ( -- For each campaign above, get the total number of subscribers and the max_subscriber_id across all its lists. - SELECT campaign_id, COUNT(subs.id) as to_send, COALESCE(MAX(subs.id), 0) as max_subscriber_id - FROM subscribers subs, subscriber_lists sublists, campaign_lists camplists - WHERE sublists.list_id = camplists.list_id AND - subs.id = sublists.subscriber_id - AND camplists.campaign_id = ANY(SELECT id FROM camps) - GROUP BY camplists.campaign_id + SELECT id AS campaign_id, COUNT(subscriber_lists.subscriber_id) AS to_send, + COALESCE(MAX(subscriber_lists.subscriber_id), 0) AS max_subscriber_id FROM camps + LEFT JOIN campaign_lists ON (campaign_lists.campaign_id = camps.id) + LEFT JOIN subscriber_lists ON (subscriber_lists.list_id = campaign_lists.list_id) + WHERE campaign_lists.campaign_id = ANY(SELECT id FROM camps) + GROUP BY camps.id ), u AS ( -- For each campaign above, update the to_send count.