ON 子句中的聚合函数无效 - 加入数组 overlap/array 包含在 Snowflake SQL 中

Invalid aggregate function in ON clause - Joining on arrays overlap/array contains in Snowflake SQL

我正在尝试根据两个数组的重叠内容将两个表连接在一起。我已经用 Posgresql 语法编写了,它有效,我正在尝试用 Snowflake SQL 编写。

下面是 posgresql 中的代码:

SELECT
COUNT(DISTINCT profiles.id ) AS count_profiles
FROM panels 
LEFT profiles  AS profiles ON ARRAY[profiles.code::text] <@ ARRAY[panels.profile_codes]

这是我在 Snowflake 中的尝试:

SELECT
COUNT(DISTINCT profiles.id ) AS count_profiles
FROM panels
LEFT JOIN profiles ON ARRAY_CONTAINS(panels.profile_codes, array_agg(profiles.code)) = 'TRUE'

但是我得到这个错误:

Invalid aggregate function in ON clause

在此先感谢您的帮助!

你有什么理由不能只使用

LEFT OUTER JOIN profiles ON panels.profile_codes = profiles.code

这里涉及到哪些数据类型和基数?
panels.profile_codesprofiles.code 中的任何数组?

ARRAY_AGG 是导致这里出现问题的聚合函数。它从您的查询中获取所有代码并将它们放入一个数组中,而不是每行一个数组。如果 profiles.code 已经是一个数组那么你应该只需要:

SELECT
COUNT(DISTINCT profiles.id ) AS count_profiles
FROM panels
LEFT JOIN profiles ON ARRAY_CONTAINS(panels.profile_codes, profiles.code)

或者您可能需要使用 TO_ARRAY:https://docs.snowflake.net/manuals/sql-reference/functions/to_array.html

... ON ARRAY_CONTAINS(panels.profile_codes, TO_ARRAY(profiles.code))

我需要了解更多您的数据,以便进一步澄清。