title: SQL distinct columns with a concat of unique
body: <p>In SQL Server, I have a simple view called <code>v_master_server_list</code>.</p>
  
  <p>Server entries have multiple group memberships, so the servers will be duplicated in the view for every group membership. I need to return only a single row with the distinct server names and for every group membership, I'd like this concatenated with some kind of delimiter like '|' or ','</p>
  
  <p>I've come up with this (thanks for the link), and I had to scrub out the null entries otherwise the LEN and LEFT functions wouldn't work. Question is, how do I now return all entries (even if nothing is found in 'arms_group')?</p>
  
  <pre><code>select server, LEFT(column_names, LEN(column_names )-1) AS column_names
  from cmdb.[dbo].v_master_server_list AS extern
  CROSS APPLY
  (
      select arms_group + ','
      FROM cmdb.[dbo].v_master_server_list AS intern
      where extern.server = intern.server
      FOR XML PATH('')
  ) pre_trimmed (column_names)
  
  where arms_group is not null
  GROUP BY server, column_names;
  </code></pre>