title: How to avoid duplicates in the STRING_AGG function
body: <p><em>My query is below:</em></p>
  
  <pre><code>select 
      u.Id,
      STRING_AGG(sf.Naziv, ', ') as 'Ustrojstvena jedinica',
      ISNULL(CONVERT(varchar(200), (STRING_AGG(TRIM(p.Naziv), ', ')), 121), '') 
      as 'Partner',
  
  from Ugovor as u
  
          left join VezaUgovorPartner as vup
              on vup.UgovorId = u.Id AND vup.IsDeleted = 'false'
          left join [TEST_MaticniPodaci2].dbo.Partner as p
              on p.PartnerID = vup.PartnerId
          left join [dbo].[VezaUgovorUstrojstvenaJedinica] as vuu
              on vuu.UgovorId = u.Id
          left join [TEST_MaticniPodaci2].hcphs.SifZavod as sf
              on sf.Id = vuu.UstrojstvenaJedinicaId
          left join [dbo].[SifVrstaUgovora] as vu
              on u.VrstaUgovoraId = vu.Id
  
    group by u.Id, sf.Naziv
  </code></pre>
  
  <p>My problem is that I can have more  sf.Naziv and also only one sf.Naziv so I have to check if there is one and then show only one result and if there is two or more to show more results. But for now the problem is when I have only one sf.Naziv, query returns two sf.Naziv with the same name because in first STRING_AGG i have more records about p.Naziv.</p>
  
  <p>I have no idea how to implement <strong>DISTINCT</strong> into <strong>STRING_AGG</strong> function</p>
  
  <p><strong>Any other solutions are welcome, but I think it should work with DISTINCT function.</strong></p>