Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get unique user engagements?
    primarykey
    data
    text
    <p>I am trying to list unique <code>user engagements</code> with the <code>engagement_type</code></p> <p>I used to have the following query.</p> <pre><code>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); </code></pre> <p>Returns:</p> <pre><code>id | fname | lname ------------------------------ 1 | joe | abc 2 | sarah | qer 3 | megan | tryey 4 | john | vdfa </code></pre> <p>Which is fine. Now, I want to include the engagment type. I've come up with this:</p> <pre><code>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); </code></pre> <p>Which now returns:</p> <pre><code>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 </code></pre> <p>The only problem with above. The results are not unique anymore. As you can see, Joe and Megan have 2 entries.</p> <p>Any idea how I can get this to work?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload