title: postgres STRING_AGG() returns duplicates?
body: <p>I have seen some similar posts, requesting advice for getting distinct results from the query. This can be solved with a subquery, but the column I am aggregating <code>image_name</code> is unique <code>image_name VARCHAR(40) NOT NULL UNIQUE</code>. I don't believe that should be necersarry. </p>
  
  <p>This is the data in the spot_images table</p>
  
  <pre><code>spotdk=# select * from spot_images;
   id | user_id | spot_id |              image_name              
  ----+---------+---------+--------------------------------------
    1 |       1 |       1 | 81198013-e8f8-4baa-aece-6fbda15a0498
    2 |       1 |       1 | 21b78e4e-f2e4-4d66-961f-83e5c28d69c5
    3 |       1 |       1 | 59834585-8c49-4cdf-95e4-38c437acb3c1
    4 |       1 |       1 | 0a42c962-2445-4b3b-97a6-325d344fda4a
  (4 rows)
  </code></pre>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT Round(Avg(ratings.rating), 2) AS rating, 
         spots.*, 
         String_agg(spot_images.image_name, ',') AS imageNames
  FROM   spots 
         FULL OUTER JOIN ratings 
                      ON ratings.spot_id = spots.id 
         INNER JOIN spot_images 
                 ON spot_images.spot_id = spots.id 
  WHERE  spots.id = 1 
  GROUP  BY spots.id; 
  </code></pre>
  
  <p>This is the result of the images row:</p>
  
  <pre><code>81198013-e8f8-4baa-aece-6fbda15a0498,
  21b78e4e-f2e4-4d66-961f-83e5c28d69c5,
  59834585-8c49-4cdf-95e4-38c437acb3c1,
  0a42c962-2445-4b3b-97a6-325d344fda4a,
  81198013-e8f8-4baa-aece-6fbda15a0498,
  21b78e4e-f2e4-4d66-961f-83e5c28d69c5,
  59834585-8c49-4cdf-95e4-38c437acb3c1,
  0a42c962-2445-4b3b-97a6-325d344fda4a,
  81198013-e8f8-4baa-aece-6fbda15a0498,
  21b78e4e-f2e4-4d66-961f-83e5c28d69c5,
  59834585-8c49-4cdf-95e4-38c437acb3c1,
  0a42c962-2445-4b3b-97a6-325d344fda4a
  </code></pre>
  
  <p>Not with linebreaks, I added them for visibility.</p>
  
  <p>What should I do to retrieve the image_name's one time each? </p>