我正在尝试根据两个数组的重叠内容将两个表连接在一起。我已经用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]
这是我在雪花中的尝试:
SELECT
COUNT(DISTINCT profiles.id ) AS count_profiles
FROM panels
LEFT JOIN profiles ON ARRAY_CONTAINS(panels.profile_codes, array_agg(profiles.code)) = 'TRUE'
但我收到此错误:
ON子句中的无效聚合函数
提前感谢您的帮助!
是否有不能仅仅使用的原因
LEFT OUTER JOIN profiles ON panels.profile_codes = profiles.code
这里涉及的数据类型和基数是什么?panels.profile_codes
或profiles.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))
我需要进一步了解您的数据,以便进一步阐明。