Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Snowflake cloud data platform 通过雪花中的distinct展平并聚合两列阵列_Snowflake Cloud Data Platform - Fatal编程技术网

Snowflake cloud data platform 通过雪花中的distinct展平并聚合两列阵列

Snowflake cloud data platform 通过雪花中的distinct展平并聚合两列阵列,snowflake-cloud-data-platform,Snowflake Cloud Data Platform,表结构是 +------------+---------+ | Animals | Herbs | +------------+---------+ | [Cat, Dog] | [Basil] | | [Dog, Lion]| [] | +------------+---------+ 所需输出(不关心此列表的排序): 第一次尝试是这样的 SELECT ARRAY_CAT(ARRAY_AGG(DISTINCT(animals)), ARRAY_AGG(herbs)) 但

表结构是

+------------+---------+
|  Animals   |  Herbs  |
+------------+---------+
| [Cat, Dog] | [Basil] |
| [Dog, Lion]| []      |
+------------+---------+
所需输出(不关心此列表的排序):

第一次尝试是这样的

SELECT ARRAY_CAT(ARRAY_AGG(DISTINCT(animals)), ARRAY_AGG(herbs))
但这会产生

[[Cat, Dog], [Dog, Lion], [Basil], []]

由于distinct在每个阵列上运行,因此不查看所有阵列中的distinct组件

您可以展平组合阵列,然后重新聚合:

SELECT ARRAY_AGG(DISTINCT F."VALUE") AS unique_things
FROM tab, TABLE(FLATTEN(ARRAY_CAT(tab.Animals, tab.Herbs))) f

您可以展平组合阵列,然后重新聚合:

SELECT ARRAY_AGG(DISTINCT F."VALUE") AS unique_things
FROM tab, TABLE(FLATTEN(ARRAY_CAT(tab.Animals, tab.Herbs))) f

如果我正确理解您的需求,并假设

insert into tabarray select array_construct('cat', 'dog'), array_construct('basil');
insert into tabarray select array_construct('lion', 'dog'), null;
我想说,结果是这样的:

select array_agg(distinct value) from
(
  select
    value from tabarray
  , lateral flatten( input => col1 )
  union all
  select
  value from tabarray
  , lateral flatten( input => col2 ))
  ;

如果我正确理解您的需求,并假设

insert into tabarray select array_construct('cat', 'dog'), array_construct('basil');
insert into tabarray select array_construct('lion', 'dog'), null;
我想说,结果是这样的:

select array_agg(distinct value) from
(
  select
    value from tabarray
  , lateral flatten( input => col1 )
  union all
  select
  value from tabarray
  , lateral flatten( input => col2 ))
  ;

这里是另一种处理空值的变体,以防它们出现在数据集中

SELECT ARRAY_AGG(DISTINCT a.VALUE) unique_things from tab, TABLE (FLATTEN(array_compact(array_append(tab.Animals, tab.Herbs)))) a

这里是另一种处理空值的变体,以防它们出现在数据集中

SELECT ARRAY_AGG(DISTINCT a.VALUE) unique_things from tab, TABLE (FLATTEN(array_compact(array_append(tab.Animals, tab.Herbs)))) a