Skip to content
Advertisement

SQL Group by JSON arrays

So I have a JSON variable (var2), which is an array. I have the following data:

I’m trying to write a query that gives back the number of dollars and the machines per group:

Example:

I’m not used to JSON arrays and it’s kinda tricky for me.

I’ve tried many things and I can’t find much documentation with examples for JSON arrays. For instance, why does this not work?

Alson can someone recommend me a book or reference with stuff like this (with JSON arrays)?

Advertisement

Answer

You need to turn the array elements into rows (essentially normalizing your data model “on-the-fly”), then you can aggregate over them:

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