Snowflake:SQL 查询以从相同或多个表中识别具有相同 ID 的所有不同用户

Snowflake: SQL Query to identify all the different users with the same ID from the same or multiple tables

有两个 table 具有 ID 和用户名。寻找一个雪花 SQL 查询,该查询可以识别来自相同 table 或两个 table 的不同用户共享的 ID。

Table 1

ID User
1001 A
1002 B
1003 C
1002 D
1005 E

Table 2

ID User
1006 H
1005 E
1003 G
1002 F
1001 A

预期结果: 1002 - [B,D,F] -> ID 1002 被两个 table 中的 3 个不同用户使用 1003 - [C,G] -> ID 1003 被两个 tables

中的 2 个不同用户使用

注意: 1001 不包括在内,因为它是来自 tables

的同一用户

使用QUALIFY:

SELECT *
FROM (SELECT Id, User
     FROM Table1
     UNION
     SELECT Id, User
     FROM Table2) sub
QUALIFY COUNT(User) OVER(PARTITION BY Id) > 1

query that results in identifying the IDs shared by different users from the same table or both tables.

这对我来说听起来像是聚合:

select id
from ((select id, user from table1) union all
      (select id, user from table2)
     ) t12
group by id
having min(user) <> max(user);