sql - Concatenate Rows using FOR XML PATH() from multiple tables -



sql - Concatenate Rows using FOR XML PATH() from multiple tables -

apologies if there reply in here, cannot find it...can concatenate rows multiple tables using xml path. allow me explain....

i have next 4 tables:

"businesssupport" has reference other 3 tables

"application", "businessprocess" , "orgaunit" (contains organizational info such service line , geography)

select a.name [app name], stuff((select ',' + bp.name [text()] businessprocess bp left bring together businesssupport bs on bp.refstr=bs.xobject bs.object=a.refstr xml path('')), 1, 1, '' ) [business process] , stuff((select ',' + org.name [text()] orgaunit org left bring together businesssupport bs on org.refstr=bs.yobject bs.object=a.refstr xml path('')), 1, 1, '' ) [service lines] application

this gives me table results such

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, have new dimension have been asked study from. table 5 "relations" has reference business back upwards , orgaunit.

i have 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 next not results returned:

stuff((select ',' + org2.name [text()] orgaunit org2 left bring together relations rel on rel.toref=org2.name left bring together businesssupport bs on bs.refstr=rel.fromref bs.object=a.refstr xml path('')), 1, 1, '' ) [geog]

thanks

if of columns returned when using for xml concatenate values homecoming null, entire concatenated string become null, since "some string" + null null sql server. if possible org2.name null in example, wrap isnull around concatenated results, , replace empty string.

stuff((select isnull(',' + org2.name, '') [text()] orgaunit org2 left bring together relations rel on rel.toref=org2.name left bring together businesssupport bs on bs.refstr=rel.fromref bs.object=a.refstr xml path('')), 1, 1, '' ) [geog]

sql concatenation

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -