Skip to content
Advertisement

Split single JSON array column and update multiple integer columns with this data

I have a table with a JSON array column containing an integer of every hour of the day, e.g.:

In the same table I have added 24 new integer columns named i0 till i23 so I can easier filter on each individual column without complicated JSON queries.

With which SQL query can I update the 24 columns with the values from the JSON array, like the example below? I need to split the JSON array into 24 values and meanwhile update each row in the same table.

I tried something like this, but then I’m getting a error ‘Query 1 ERROR: ERROR: cannot cast type json to integer’

Advertisement

Answer

I fixed it using the query below. Is there a better method?

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