I have a my_table like this. Col2 is a string type:
| Col1 | Col2 |
|--------------|------------------------|
| some_string |{key1:0, key2:1, key3:0}|
| some_string |{key1:0, key2:0, key3:0}|
| some_string |{key1:1, key2:2, key3:3}|
| some_string |{key1:1, key2:1, key3:0}|
I would like to use SQL to parse the key:value pairs on Col2, and filter out pairs where the value is "0" so that the output is:
| Col1 | Col2 |
|--------------|------------------------|
| some_string |{key2:1} |
| some_string |{} |
| some_string |{key1:1, key2:2, key3:3}|
| some_string |{key1:1, key2:1} |
Would someone help?
2
Answers
Here’s an approach you can take using
Redshift
SQL:This is a bit complex because Redshift doesn’t support dynamic string parsing into columns or rows as easily as newer SQL databases. Typically, you’d handle such data in the application layer or use a database with JSON processing capabilities.
Since Redshift is a Postgres-based data warehouse This query might be working for you.
Initially, we plit the Col2 into key-value pairs, then filter out elements that have a value of 0, and finally, we rebuild Col2 :
Results :
Demo on PostgreSql