Apologies if there was an answer in here, but I cannot find it...Can you concatenate rows from multiple tables using FOR XML PATH. Let me explain....
I have the following 4 tables:
"BusinessSupport" which has reference to the other 3 tables
"Application", "BusinessProcess" and "OrgaUnit" (contains organizational info such as Service Line and Geography)
SELECT a.NAME AS [App Name],
STUFF((SELECT ',' + bp.NAME as [text()]
FROM BUSINESSPROCESS bp
LEFT JOIN BUSINESSSUPPORT bs on bp.REFSTR=bs.XOBJECT
WHERE bs.OBJECT=a.REFSTR FOR XML PATH('')), 1, 1, '' ) AS [Business Process] ,
STUFF((SELECT ',' + org.NAME as [text()]
FROM ORGAUNIT org
LEFT JOIN BUSINESSSUPPORT bs on org.REFSTR=bs.YOBJECT
WHERE bs.OBJECT=a.REFSTR FOR XML PATH('')), 1, 1, '' ) AS [Service Lines]
FROM APPLICATION a
This gives me table results such as
AppName; Business Process; Service Lines
app1; process1, process2, process 3; SL1, SL2, SL3
app2; process1, process 2; SL1, SL4, SL4
app3; process4, process 2; SL3, SL5, SL6
However, I now have a new dimension I have been asked to report from. Table 5 "Relations" which has a reference to Business Support and Orgaunit.
I would like to have something like
AppName; Business Process; Service Lines; Geography
app1; process1, process2, process 3; SL1, SL2, SL3; Geography1, Geography2
app2; process1, process 2; SL1, SL4, SL4; Geography1, Geography3
app3; process4, process 2; SL3, SL5, SL6; Geography3, Geography4, Geography5
I have tried the following but do not get any results returned:
STUFF((SELECT ',' + org2.name as [text()]
FROM ORGAUNIT org2
LEFT JOIN RELATIONS rel ON rel.TOREF=org2.name
LEFT JOIN BUSINESSSUPPORT bs on bs.REFSTR=rel.FROMREF
WHERE bs.OBJECT=a.REFSTR FOR XML PATH('')), 1, 1, '' ) AS [Geog]
Thanks