SELECT ci.ciname AS 'Name',
(SELECT rel.relationship + Char(10) + ' ' + ci2.ciname
+ Char(10) FROM cirelationships AS cirel
LEFT JOIN ci AS ci2
ON ci2.ciid = cirel.ciid2
LEFT JOIN relationshiptype AS rel
ON rel.relationshiptypeid = cirel.relationshiptypeid
WHERE cirel.ciid = ci.ciid
FOR xml path('')) AS 'Relationship Details' FROM ci
WHERE ciid IN (SELECT ciid
FROM cirelationships)
SELECT ci.ciname AS "Name",
(SELECT Array_to_string(Array_agg(Concat(rel.relationship, ' ',
ci2.ciname)),
', ')
FROM cirelationships AS cirel
LEFT JOIN ci AS ci2
ON ci2.ciid = cirel.ciid2
LEFT JOIN relationshiptype AS rel
ON rel.relationshiptypeid = cirel.relationshiptypeid
WHERE cirel.ciid = ci.ciid) "Relationship Details" FROM ci
WHERE ciid IN (SELECT ciid
FROM cirelationships)