This query returns all the rows that I will define as "recalls". See screenshot for results. I have tried and thus far failed to figure out how to sum the number of rows (each a distinct recall) per each id. In the results screenshot below there is only one id shown: 61401. But there are others IDs lower in the actual results. I'd like to count the number of rows(recalls) per unique id. I keep running into aggregate errors. I tried sub queries and 'group by' but just can't figure it out. Any ideas how I can do this? Ultimately I want two columns: id and recall_count.
SELECT
prr_u.id,
cprr_r.created_at AS recall_create_date,
prr_m.created_at AS trial_start_date,
prr_m.trial_days AS trial_period_length,
prr_m.created_at + interval '1 day' * trial_period_length AS trial_end_date
FROM my_db.productionrr_users AS prr_u
INNER JOIN my_db.productionrr_memberships AS prr_m
ON prr_u.id = prr_m.id
INNER JOIN my_db.c4productionrr_recalls AS cprr_r
ON cprr_r.user_id = prr_m.id
WHERE recall_create_date BETWEEN trial_start_date AND trial_end_date;
Here is a snippet of the result:
I'd like to see results that look like this (where recalls_count is simply the number of rows returned per id):
I also tried Tan's suggestion here and get the aggregate DB Errorcode=500310.
SELECT
prr_u.id as id,
cprr_r.created_at AS recall_create_date,
prr_m.created_at AS trial_start_date,
prr_m.trial_days AS trial_period_length,
prr_m.created_at + interval '1 day' * trial_period_length AS trial_end_date,
COUNT(DISTINCT recall_create_date) as recall_count
FROM my_db.productionrr_users AS prr_u
INNER JOIN my_db.productionrr_memberships AS prr_m
ON prr_u.id = prr_m.id
INNER JOIN my_db.c4productionrr_recalls AS cprr_r
ON cprr_r.user_id = prr_m.id
WHERE recall_create_date BETWEEN trial_start_date AND trial_end_date
GROUP BY prr_u.id;
Also, I am able to count the total rows within the query but I still can't do it for each id:
SELECT COUNT(*) FROM (
SELECT prr_u.id, prr_u.email, prr_m.status, cprr_r.created_at AS recall_create_date, prr_m.created_at AS trial_start_date, prr_m.trial_days AS trial_period_length, prr_m.created_at + interval '1 day' * trial_period_length AS trial_end_date, prr_m.expires_at
FROM my_db.productionrr_users AS prr_u
INNER JOIN my_db.productionrr_memberships AS prr_m
ON prr_u.id = prr_m.id
INNER JOIN my_db.c4productionrr_recalls AS cprr_r
ON cprr_r.user_id = prr_m.id
WHERE recall_create_date BETWEEN trial_start_date AND trial_end_date
);
Output is the total count of:
@gmb I am unable to get past this aggregate function error. I have tried this as I cannot run your code as it is:
SELECT prr_u.id,
prr_m.created_at + interval '1 day' * prr_m.trial_days AS trial_end_date,
COUNT(*) AS recalls_count
FROM my_db.productionrr_users AS prr_u
INNER JOIN my_db.productionrr_memberships AS prr_m
ON prr_u.id = prr_m.id
INNER JOIN my_db.c4productionrr_recalls AS cprr_r
ON cprr_r.user_id = prr_m.id
WHERE cprr_r.created_at BETWEEN prr_m.created_at AND trial_end_date
GROUP BY prr_u.id;
which produces the following aggregate error:
`