[英]Accessing array column in SQL - for multiple values
I have a Snowflake database that has an array column 'EQUIVALENT' (correct terminology?) as per the below jpg我有一个雪花数据库,它有一个数组列'EQUIVALENT'(正确的术语?)根据下面的 jpg
I worked out I can access this for a single value, using the code below,我发现我可以使用下面的代码访问单个值,
WHERE EQUIVALENT[0] IN('A') OR EQUIVALENT[1] IN('A') OR EQUIVALENT[2] IN('A')
But, this is clumsy, and I need 10 OR statements to look in each location.但是,这很笨拙,我需要 10 个 OR 语句来查看每个位置。 I actually need to pass 200 Equivalents, and find the resulting 'Part's, therefore was hoping for a variant of the following that would return the found EQUIVALENT and related PART
我实际上需要传递 200 个等价物,并找到生成的“零件”,因此希望以下变体可以返回找到的等价物和相关零件
SELECT * FROM ..... WHERE EQUIVALENT IN('B','ZZ','G'....)
. . I'm relatively new to SQL, therefore the terminology in Snowflake help is really not helping!
我对 SQL 比较陌生,因此 Snowflake 帮助中的术语真的没有帮助!
You can use array functions .您可以使用数组函数。
where array_size(array_intersection(equivalent, array_construct('B', 'ZZ', 'G' . . .))) > 0
You can use ARRAYS_OVERLAP(<array1>, <array2>):您可以使用 ARRAYS_OVERLAP(<array1>, <array2>):
Compares whether two arrays have at least one element in common.比较两个 arrays 是否具有至少一个共同元素。 Returns TRUE if there is at least one element in common;
如果至少有一个共同元素,则返回 TRUE; otherwise returns FALSE.
否则返回 FALSE。 The function is NULL-safe, meaning it treats NULLs as known values for comparing equality.
function 是 NULL 安全的,这意味着它将 NULL 视为比较相等性的已知值。
WHERE ARRAYS_OVERLAP(ARRAY_CONSTRUCT('B', 'ZZ', 'G', ...), equivalent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.