0

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:

enter image description here

I'd like to see results that look like this (where recalls_count is simply the number of rows returned per id):

enter image description here

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:

enter image description here

@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:

enter image description here

`

Kierk
  • 476
  • 6
  • 23
  • 2
    Please show us the result that you expect. – GMB Apr 29 '20 at 18:06
  • Please [use text, not images/links, for text--including tables & ERDs](https://meta.stackoverflow.com/q/285551/3404097). Use images only for what cannot be expressed as text or to augment text. Include a legend/key & explanation with an image. – philipxy Apr 30 '20 at 00:44
  • Please in code questions give a [mre]--cut & paste & runnable code, including smallest representative example input as code; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. Give the least code you can give that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) For SQL that includes DBMS & DDL (including constraints & indexes) & input as code formatted as a table. [ask] Stop trying to code your overall goal & for 1 problem query say what you expected instead from the given code & why. – philipxy Apr 30 '20 at 00:46
  • I tried to add results in text but was too messy. – Kierk Apr 30 '20 at 00:47
  • Just do it. Use code block format. (Read the edit help.) PS Both the code & data here can be much smaller for each problem query. (Post 1 question per 1 query.) PS Very likely your question is just a duplicate re basics of grouping & aggregation & you don't even need to post. Before considering posting please read the manual & google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. Reflect your research. See [ask] & the voting arrow mouseover texts. – philipxy Apr 30 '20 at 00:53
  • I am new to sql. So I wanted to include all the elements of the query as I wasn't sure if Join was effecting GROUP BY in a way I don't grasp. While your comment has validity, it does not serve my level of understanding of SQL. So I did not want to strip it down. Also, I absolutely could not find the answer. That is because I was selecting a column with unique values, which was confusing me. Long story. But I stand by my need to include the query as is without stripping it down. I could not figure this without some of the guidance I received here in the context of my post and query. – Kierk May 01 '20 at 09:53

3 Answers3

1

It looks like you just want aggregation:

SELECT prr_u.id, COUNT(*) 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 recall_create_date BETWEEN trial_start_date AND trial_end_date
GROUP BY ppr_u.id;
GMB
  • 216,147
  • 25
  • 84
  • 135
  • How do I fit this in though without getting aggregate error? ````prr_m.created_at + interval '1 day' * trial_period_length AS trial_end_date```` – Kierk Apr 29 '20 at 18:40
  • @Kierk: this column does not belong to the expected result that you have showned. – GMB Apr 29 '20 at 18:43
  • That column is needed for the join and where clauses to produce the desired output. But it need not be in the output. – Kierk Apr 29 '20 at 19:26
  • @Kierk: the above query uses the same `where` clause and join conditions as your original query. It should produce the results that you expect. – GMB Apr 29 '20 at 20:38
  • I added my attempt based on your suggestion to my question. – Kierk Apr 29 '20 at 21:00
0
Select prr_u.id as id, count(distinct recall_create_date) as recall_count... Group by prr_u.id 

This code groups according to id then counts the number of records with that specific id. In the code where you grouped using all columns, each group consists of each recall entry instead of a group of recall entries under the same id.

edit: You seem to be getting the aggregate error because you cannot select additional columns without grouping by them (or using an aggregate function on them). See: Aggregate function error while using group by clause in SQL.

  • Thanks. I added my interpretation of your suggestion to my post. Is this what you were thinking? It gives me the aggregate function error: 500310. – Kierk Apr 29 '20 at 18:29
0

I figured out the answer. But I don't need to show the trial end date column but I need that column to identify the end_date. My issue all along was that I was adding a non unique column to my select and group by so every 'recall' was counted once and not aggragated.

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, trial_end_date;
```
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/JmzWg.png
Kierk
  • 476
  • 6
  • 23
  • Please look at the formatted version of your post before posting. PS Use text not links/images for text. – philipxy Apr 30 '20 at 00:55