I have one big table in a snowflake db which I want to split into smaller tables according to a column while flattening one column into many columns.
The big table shows animals of three categories (lion, tiger, zebra). I want to split it up into a separate lion, tiger and zebra table. On top I want to flatten a json blob (column "Details") into different columns.
How can I do that?
One way to do it is to write a user defined function with snowpark (Python), convert the table to a pandas DataFrame and then use normal Python code. I think there is a simpler way without the costly transformation to a pandas DataFrame. Maybe there even is a solution in pure SQL.
Original table
| Animal | Name | Details (json blob) |
|---|---|---|
| Lion | Georg | lion key1: value1, lion key2: value2 |
| Tiger | John | tiger key1: value1, tiger key2: value2, tiger key3: value3 |
| Lion | Patrick | lion key1: value1, lion key2: value2 |
| Tiger | Sam | tiger key1: value1, tiger key2: value2, tiger key3: value3 |
| Lion | Paul | lion key1: value1, lion key2: value2 |
| Zebra | Sarah | zebra key1: value1 |
New table: Lion table
| Name | lion key1 | lion key2 |
|---|---|---|
| Georg | value1 | value2 |
| Patrick | value1 | value2 |
| Paul | value1 | value2 |
New table: tiger table
| Name | tiger key1 | tiger key2 | tiger key3 |
|---|---|---|---|
| John | value1 | value2 | value3 |
| Sam | value1 | value2 | value3 |
New table: zebra table
| Name | zebra key1 |
|---|---|
| Sarah | value1 |
