I am trying to list unique user engagements
with the engagement_type
I used to have the following query.
SELECT u.id, u.fname, u.lname FROM (
SELECT id, engagement_type FROM (
SELECT user_id AS id, 'comment'
FROM comments WHERE commentable_id = 48136 AND commentable_type = 'Video'
UNION ALL
SELECT user_id AS id, 'like'
FROM likes WHERE likeable_id = 48136 AND likeable_type = 'Video'
) AS a
GROUP BY id
LIMIT 10
) b JOIN users u USING (id);
Returns:
id | fname | lname
------------------------------
1 | joe | abc
2 | sarah | qer
3 | megan | tryey
4 | john | vdfa
Which is fine. Now, I want to include the engagment type. I've come up with this:
SELECT u.id, u.fname, u.lname, engagement_type FROM (
SELECT id, engagement_type FROM (
SELECT user_id AS id, 'comment' AS engagement_type
FROM comments WHERE commentable_id = 48136 AND commentable_type = 'Video'
UNION ALL
SELECT user_id AS id, 'like' AS engagement_type FROM likes
WHERE likeable_id = 48136 AND likeable_type = 'Video'
) AS a
GROUP BY id, engagement_type
LIMIT 10
) b JOIN users u USING (id);
Which now returns:
id | fname | lname | engagement_type
---------------------------------------------------
1 | joe | abc | comment
2 | sarah | qer | like
3 | megan | tryey | like
4 | john | vdfa | like
1 | joe | abc | like
3 | megan | tryey | comment
The only problem with above. The results are not unique anymore. As you can see, Joe and Megan have 2 entries.
Any idea how I can get this to work?