我在雪花中有这样的观点:
create or replace view foo as
select $1 as id_foo, $2 as sales
from (values (1,100),(2,200),(3,300));
和这个用户定义的表函数 https://docs.snowflake.com/en/sql-reference/udf-table-functions.html:
CREATE OR REPLACE FUNCTION build_aux_table ( restriction number )
RETURNS TABLE ( aux_id number )
AS 'select $1 as aux_id from (VALUES (1),(2),(3)) where aux_id = restriction';
以下查询有效,并返回 3 行:
select id_foo, baz.aux_id
from foo
cross join table(build_aux_table(foo.id_foo)) baz;
但是,我没想到查询会编译,因为我们要连接的 UDTF 生成的表取决于第一个表中的列。我的理解是这种表内依赖需要一个LATERAL https://docs.snowflake.com/en/sql-reference/constructs/join-lateral.html加入,如下所示(也有效):
select id_foo, baz.aux_id
from foo
cross join lateral build_aux_table(foo.id_foo) baz;
为什么没有 LATERAL 的查询可以工作?