I have a Redshift table with an example row with the following structure:
id url
12345 http://www.things.com/details/?foo=hello&bar=world&baz=John+Smith
45678 http://www.things.com/details/?foo=hello&bar=america&booz=Howard+Jones&other_field=Portugal
I would like to extract everything after the ?
in the url and for every &
, add the value on the left of the =
to a keys column and the value on the right to a values column. There is no determinate number of &
's in a given URL. The desired output is as follows:
id key value
12345 foo hello
12345 bar world
12345 baz John+Smith
45678 foo hello
45678 bar america
45678 booz Howard+Jones
45678 other_field Portugal
My solution right now is to pick a reasonably high number and to write a Python script that writes the same query with a new UNION ALL
for each integer. I parse out the desired fields using SPLIT_PART(SPLIT_PART(SPLIT_PART(url, '?', 2), '&', {i}), '=', 1)
and SPLIT_PART(SPLIT_PART(SPLIT_PART(url, '?', 2), '&', {i}), '=', 2)
during each iteration.