I have a dataset like this:
| userid | productid | score |
|---|---|---|
| A | 1 | 4 |
| A | 2 | 4 |
| A | 3 | 5 |
| B | 1 | 4 |
| B | 2 | 4 |
| B | 3 | 5 |
I want to have an output like this:
| userid1 | userid2 | matching_product |
|---|---|---|
| A | B | 1 2 3 |
but I'm only able to get the first two column with this query:
CREATE TABLE score_greater_than_3 AS
SELECT userid, productid, score
FROM reviews
WHERE score >= 4;
SELECT s1.userid as userid1, s2.userid as userid2
FROM score_greater_than_3 s1
INNER JOIN score_greater_than_3 s2 ON s1.productid=s2.productid AND s1.userid<s2.userid
GROUP BY s1.userid, s2.userid
HAVING count(*)>=3;
How can i get the matching products? i'm ok with an output like this too if its more easy
| user1 | user2 | matched product |
|---|---|---|
| a | b | 1 |
| a | b | 2 |
| a | b | 3 |