基于 Hive 中的三个连接条件在两个表之间连接

Join between two tables based on three joining conditions in Hive

我想要低于期望的输出(输出 table),如下图所示。 我正在使用以下查询,但在配置单元中收到笛卡尔积是不安全功能的警告。

select
t1.securitykey as securitykey,
t2.sector as sector,
t2.industrysubgroup as industrysubgroup  
from table1 t1 left join table2 t2 on (t1.securitykey=t2.key1 or t1.securitykey=t2.key2 or t1.securitykey= t2.key3);

table1

SecurityKey
1
2
3
4

table2

key1 key2 key3 sector industrysubgroup
1 7 9 Electronics US electronincs
5 2 10 Industrial Defense
6 8 3 Consumer entertainment

table3

key1 sector industrysubgroup
1 Electronics US electronincs
2 Industrial Defense
3 Consumer entertainment
4 (null) (null)

能请教一下吗?

看不出有什么问题。它适用于您的示例。也许测试数据缺少一些东西。

WITH table1 AS
  (SELECT 1 AS securityKey
   UNION ALL SELECT 2
   UNION ALL SELECT 3
   UNION ALL SELECT 4),
     table2 AS
  (SELECT 1 AS key1,
          7 AS key2,
          9 AS key3,
          'electronics' AS sector,
          'us electronics' AS industrysubgroup
   UNION ALL SELECT 5,
                    2,
                    10,
                    'indus',
                    'defense'
   UNION ALL SELECT 6,
                    8,
                    3,
                    'consumer',
                    'entertainment')
SELECT t1.securitykey AS securitykey,
       t2.sector AS sector,
       t2.industrysubgroup AS industrysubgroup
FROM table1 t1
LEFT JOIN table2 t2 ON (t1.securitykey=t2.key1
                        OR t1.securitykey=t2.key2
                        OR t1.securitykey= t2.key3);

你可以试试

select * from table1 as t1
left join (
     select key1 as securitykey, sector, industrysubgroup from table2
     union all
     select key2 as securitykey, sector, industrysubgroup from table2
     union all
     select key3 as securitykey, sector, industrysubgroup from table2
 ) as t2 on t1.securitykey=t2.securitykey

按不同的列连接 3 次并使用 coalesce() 或 case 表达式:

select
t1.securitykey as securitykey,
coalesce(t2.sector,t3.sector,t4.sector) as sector,
coalesce(t2.industrysubgroup,t3.industrysubgroup,t4.industrysubgroup) as industrysubgroup  
from table1 t1 
     left join table2 t2 on t1.securitykey=t2.key1 
     left join table2 t3 on t1.securitykey=t2.key2
     left join table2 t4 on t1.securitykey=t2.key3;