Snowflake cloud data platform 雪花sql中的postgresSQL unnest()等效于什么

Snowflake cloud data platform 雪花sql中的postgresSQL unnest()等效于什么,snowflake-cloud-data-platform,unnest,Snowflake Cloud Data Platform,Unnest,如何在snowflake中修改PostgresSQL UNNEST(array[ 'x' || to_char(date_trunc('MONTH', max(date)), 'Mon YYYY' , 'y' || to_char(date_trunc('MONTH', max(date)), 'Mon YYYY') )]) 可以使用“展平”从数组中分解值,然后使用“表格”将值转换为表格: -- Use an array for testing: select arr

如何在snowflake中修改PostgresSQL

UNNEST(array[
    'x' || to_char(date_trunc('MONTH', max(date)), 'Mon YYYY' ,
    'y' || to_char(date_trunc('MONTH', max(date)), 'Mon YYYY')
    )])

可以使用“展平”从数组中分解值,然后使用“表格”将值转换为表格:

-- Use an array for testing:
select array_construct(1, 2, 3, 4, 5);

-- Flattens into a table with metadata for each row:
select * from table(flatten(input => array_construct(1, 2, 3, 4, 5)));

--Pulls out just the values from the array:
select value::integer from table(flatten(input => array_construct(1, 2, 3, 4, 5)));
“::integer”
部分将值从数组中转换为所需的数据类型。这是可选的,但建议使用

您可以通过创建用户定义的表函数来近似unnest的语法:

create or replace function UNNEST(V array)
returns table ("VALUE" variant)
language SQL
aS
$$
    select VALUE from table(flatten(input => V))
$$;
你可以这样称呼它:

select * from table(unnest(array_construct(1, 2, 3, 4, 5)));

这将返回一个表,其中有一列名为variant类型的VALUE。您可以制作一个返回字符串、整数等的版本。

谢谢Greg。我使用了flatte(),但一直遇到一个错误:SQL编译错误:未知函数flatte。必须将flatte用作表函数。这意味着它只能在TABLE()函数的括号内工作。如果在table()函数的括号外使用,则会出现该错误。