跨表比较数组

Compare arrays across tables

我正在查询一些 data (SQL, presto),我正在努力加入 2 table。我需要根据标签 table 中列出的团队标签了解问题与哪些团队相关。每个问题都可能与多个团队相关联。

Table 1 team:

name  | tags
------+--------------------
team1 | [1234, 2345, 5678]
team2 | [6789, 4321]
team3 | [4530]
team4 | [6532]
team5 | [3452, 34234, 868686]

Table 2 issues:

issue_id | tags
---------+--------------------------------------------
   1     | [312312, 45345, 4535,1111,4533,4530, 4321 ]
   2     | [312312, 45345, 6532]
   3     | [6532]
   4     | [312312, 1234, 4321]
   5     | [312312]

我需要它看起来像:

issue_id | team
---------+--------
1        | team3
1        | team2
2        | team4
3        | team4
4        | team1
4        | team2

我想避免硬编码,因为 teams 的列表及其关联的 tags 可以是动态的,但不确定如何解决这个问题

下面的SQL将两个表中的tag的值进行爆破,将数组转化为单值,这样就可以跨2表join来查询和展示teams及其对应的issues。

  Select T2.issue_id as issue_id,
             T1.name as team
  from
   (SELECT name, tag
    FROM table1
    CROSS JOIN UNNEST(tags) AS t (tag)) T1
   JOIN
  (SELECT issue_id, tag
    FROM table2
    CROSS JOIN UNNEST(tags) AS t (tag)) T2
  ON T1.tag = T2.tag;

我按照你在 Snowflake 中的例子做了 flatten 函数。从我在 presto 的文档中看到的,它也有。也许这会有所帮助?

在 Snowflake 中,我已经满足了您的要求。

with ISSUES as(

SELECT 
    T.ISSUE_ID,
    X.VALUE::INTEGER AS TAGS
FROM PUBLIC.ISSUES T, LATERAL FLATTEN(T.TAGS) X),

TEAMS as(
SELECT 
    T.TEAM,
    X.VALUE::INTEGER AS TAGS
FROM PUBLIC.TEAMS T, LATERAL FLATTEN(T.TAGS) X)


SELECT 
   ISSUES.ISSUE_ID, 
   TEAMS.TEAM FROM TEAMS
INNER JOIN ISSUES on TEAMS.TAGS = ISSUES.TAGS