Skip to content
Advertisement

Turn Cross Apply opensjson results into columns

We have a table that has a field with some json. using cross apply I was able to turn those pairs into columns:

Result

So how do I turn this into something like :

enter image description here

Thanks.

Advertisement

Answer

If you know the limited set of possible key names in advance, you can just use PIVOT:

But you can’t write a PIVOT if you don’t know all those possible values up front. So it’s a wee bit more complicated and requires dynamic SQL (I wrote an article about dynamic PIVOT here):

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