title: Duplicates results in query
body: <p>I have an <code>apps</code> table. Each app has many <code>conversations</code> and <code>users</code>. A conversation has many <code>messages</code> and each message can either belong to a <code>visitor</code> or <code>user</code> and a <code>visitor</code> can have many conversations.</p>
  
  <p>For each of my conversations, I want to attach the name and avatar of the user who most recently wrote in the conversation.</p>
  
  <p>If no user has replied, then instead I'd like to grab the 3 most recently created user's <code>avatars</code>, along with the name of the app, and use these instead.</p>
  
  <p>This is what I've got so far, but it returns multiple results for the same conversation id, and I haven't found a solution to getting the app users avatars</p>
  
  <pre><code>select
          c.id,
          c.last_message,
          c.last_activity,
          coalesce(last.display_name, a.name || ' Team') as name,
          array_agg(last.avatar)
        from messages m
        left join conversations c on c.id = m.conversation_id
        left join apps a on a.id = c.app_id
        left join lateral (
            select u.id, u.display_name, u.avatar
            from users u
            where u.id = m.user_id
        ) as last on true
        where c.visitor_id = 'c6p77hu9v000a4zcth4lnefn9'
        group by c.id, last.display_name, last.avatar, a.name
        order by c.inserted_at desc
  </code></pre>
  
  <p>Any help is greatly appreciated </p>