如何为 "IN" 条件大查询提供基于变量的搜索

How to provide variable based search for "IN" condition Big Query

我正在为此查询使用 BigQuery 控制台,它工作正常:

SELECT values, cities FROM x.y.z WHERE values IN (1,2,3)

我不确定为什么这没有按预期工作

declare var1 Array<INT64>;
set var1 = (SELECT ARRAY(SELECT distinct values FROM `a.b.c`));
SELECT values, cities FROM x.y.z WHERE values IN UNNEST(var1)

有什么帮助吗?

您只需改用内部联接:

select values, cities from x.y.z inner join (select distinct values as dv from a.b.c) t on values = dv

试试这个

declare var1 Array<INT64>;
set var1 = (SELECT ARRAY_AGG(distinct values) FROM `a.b.c`);
SELECT values, cities FROM x.y.z WHERE values IN UNNEST(var1)

您刚刚在第二行中添加了额外的 SELECT - 请尝试以下

declare var1 Array<INT64>;
set var1 = ARRAY(SELECT distinct values FROM `a.b.c`);
SELECT values, cities FROM x.y.z WHERE values IN UNNEST(var1)