I have an xml column, geographyIdXml
, in table table1
.
The xml in that column looks like this:
<args>
<arg val="1" />
<arg val="2" />
miss a few..
<arg val="99" />
<arg val="100" />
</args>
The attribute val
in each <arg/>
tag refers to the id column of rows in the geographies
table.
I want to return a few columns from table1 and the geography names that relate back to the geographyIdXml field -- in a single row AND in a single column.
For example, if these are the columns returned:
id, name, geographies
the data will look like:
1, 'My spectacular campaign', 'New York, Paris, Peckham'
I've tried all afternoon to get this working.
Tried
cross apply geographyIdXml.nodes('args/arg') as ids(id)
inner join core..tbl_geography g on g.id = ids.id.value('@val','int')
but that just returns one row per entry in the xml
Tried the above and coalescing, but I couldn't get it to work.
If anyone has any ideas, I'm all ears. Thanks.
------ UPDATE - SQL EXAMPLE ---------
select r.*, coalesce(g.name + ', ','') as name
from campaignRun r
cross apply geographyIdXml.nodes('args/arg') as ids(id)
inner join geographies g on g.id = ids.id.value('@val','int')
where r.id = 1
I get ten rows returned - one for each node in the geographyIdXml field - which is wrong.
What I want is one row with a coalesced field of all geographies but somehow, I need to join my geographies
table to my campaignRun
table using the xml in the geographyIdXml
field.
Thanks for any help you can offer