In SQL Server, I have a simple view called v_master_server_list
.
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 ','
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')?
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;