Skip to content
Advertisement

FOR XML in SQL Server: Elements based on common values of columns

I’m using FOR XML to create an XML file out of SQL queries. I want to have elements based around common values for a particular column, in this case “location”:

I can easily convert his into XML row-by-row like so:

Which produces XML that looks liks this:

But what I actually want is this:

Advertisement

Answer

You can use an ad hoc derived table that gets the distinct locations, select the locations from it and get the teams and scores in a correlated subquery.

db<>fiddle

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement