Skip to content
Advertisement

Transform and group row data into columns

I’m looking for a way to combine different rows into a single row with columns.

Let’s take this example table:

Desired output:

Please note that Data centers are not necessarily these three, they can be N different data centers that I will not know in advance.

Advertisement

Answer

With a known set of sourceDataCenter values, you can just use a simple PIVOT operator:

If you don’t know the names of the data centers in advance, you need to use dynamic SQL to generate an equivalent query, first by getting the distinct list of values, and then adding those into the two relevant spots in the query:

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