Both are working almost same
lets try with below example
- creating a table in snowflake with semi structure(json) data
/* created a table to perfrom query */
create or replace table gs_table_customer as
select column1 as cust_id, parse_json(column2) as cust_details
from values
(29711,'{ name: [{ first: "Ram", last: "Ji"}],
contact: [
{ business:[
{ type: "phone", content:"123-3210" },
{ type: "email", content:"[email protected]" } ] } ] }'),
(29722,'{ name:[{ first: "Hanuman", last: "Ji"}],
contact: [
{ business:[
{ type: "phone", content:"234-4321" },
{ type: "email", content:"[email protected]" } ] } ] }') v;
- Verify result set
/* Verify result */
select * from gs_table_customer
- Example 1 - Difference between LATERAL FLATTEN(...) and TABLE(FLATTEN(...)) in Snowflake
/* query to fetch contact details via lateral flatten */
SELECT cust_id as "ID",
--f.value AS "Contact",
f1.value:type AS "Type",
f1.value:content AS "Details"
FROM gs_table_customer tbl,
lateral flatten(input => tbl.cust_details, path => 'contact') f,
lateral flatten(input => f.value:business) f1;
/* query to fetch contact details via TABLE(FLATTEN) */
SELECT cust_id as "ID",
--f.value AS "Contact",
f1.value:type AS "Type",
f1.value:content AS "Details"
FROM gs_table_customer tbl,
TABLE(FLATTEN(input => tbl.cust_details, path => 'contact')) f,
TABLE(FLATTEN(input => f.value:business)) f1;
- Example 2 - Difference between LATERAL FLATTEN(...) and TABLE(FLATTEN(...)) in Snowflake
/* query to fetch name details via lateral flatten */
SELECT cust_id as "ID",
--f.value AS "name_details",
f.value:first,
f.value:last
FROM gs_table_customer tbl,
lateral flatten(input => tbl.cust_details, path => 'name') f
/* query to fetch name details via TABLE(FLATTEN()) */
SELECT cust_id as "ID",
--f.value AS "name_details",
f.value:first,
f.value:last
FROM gs_table_customer tbl,
TABLE(FLATTEN(input => tbl.cust_details, path => 'name')) f