Skip to content

Commit 28bc3eb

Browse files
Update using-transform-function-in-snowflake-to-extract-values-from-a-json-array.md
1 parent 9a43c3e commit 28bc3eb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

using-transform-function-in-snowflake-to-extract-values-from-a-json-array.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ order_id | coupons_applied
3232
444 |  
3333
444 |  
3434

35+
### Using the TRANSFORM function with a Lamba expression
36+
3537
We can using the TRANSFORM function to apply a Lambda Fuction to each element in the JSON array to extract the Coupon as following:
3638

3739
```sql
@@ -50,3 +52,17 @@ FROM orders;
5052
| 222 | [ "ccc" ] |
5153
| 333 | [ "ccc", "aaa", "eee" ] |
5254
| 444 | |
55+
56+
### Using the Lateral Flatten
57+
58+
The same can be achieved using the Lateral Flatten in Snowflake:
59+
60+
```sql
61+
62+
select order_id, listagg (f.value:Coupon, ', ')
63+
from orders
64+
, lateral flatten(input => parse_json(orders.coupon_json_array)::variant, OUTER => TRUE) as f
65+
group by all
66+
```
67+
68+
How this will be a lot slower compare to using the TRANSFORM function.

0 commit comments

Comments
 (0)