I want to join the result of two different queries from two different tables based on a common column (which is named differently in the two tables) but I cant seem to figure out the easy way to do it. I'm using psycopg2 on python 3.5.
From the first table, I need the Count of meetings. So I do:
SELECT user_id,COUNT(1) as num
FROM tasks
WHERE created_at BETWEEN %s and %s
GROUP BY user_id """,(DAYS_AGO_7_CONFIG,YESTERDAY))
This gives me
UserID | Number
1 | 123
2 | 12
UserID
is my mutual column. Now I made userID
into a list by list comprehension, then I do:
SELECT id, given_name,family_name,email FROM users WHERE id in +str(userID))
and I get the id
, given_name
, family_name
, and email
fields from the second table.
Now I need to add the Number column into this result, which I'm doing by very inefficient non-DB means (pandas library in python)
I'm pretty sure this entire thing can be done in one query (perhaps with union?), but I cant seem to work it out.
Intended output:
id | given_name | family_name | email | Number