I am extracting some database data using PostgreSQL via Grafana.
The first SQL gives me a list of user_ids (User2, User3…) who have interacted with a specific User1:
SELECT DISTINCT user_id AS user
FROM transactions
WHERE (type_id, operation_id) IN (
SELECT type_id, operation_id
FROM transactions
WHERE user_id = 11111 --This is User1
)
AND profile_id != 11111;
The second SQL gives me some stats for ONE specific user_id (e.g. User2) from another table:
SELECT
COUNT(DISTINCT operation_id) AS "# of operations"
FROM
operations
WHERE
user_id = 22222 --This is User2
How can I “merge” both queries, so that, for each of the users retrieved from the 1st query (User2, User3…) I can get the stats on the second query (rather than only for a hardcoded User2)?
Thanks!