2016-12-02 68 views
0

我有一个关于数据库查询的问题。请参考下表。从多列中查找列

Table : 1 
ID Country 
1 x 
2 y 
3 z 
4 k 


Table : 2 
eng fre fre1 fre2 
x x 
x1 k  y t 
x2   n  z 

Output Table 
id country 
1 x 
2 x1 
3 x2 
4 x1 

如何在Hive中实现此目的?

非常感谢您的帮助。

回答

1

你可以参加3次,但它可能会运行缓慢:因为蜂巢没有支点部

select a.id, coalesce(b.eng, c.eng, d.eng) as Country 
    from table_1 a 
     left join table_2 b on a.country=b.fre 
     left join table_2 c on a.country=c.fre1 
     left join table_2 d on a.country=d.fre2 
; 
+0

第一选项将不会在我的情况下工作。剩下的选项是我已经使用的第二个选项,但正如你所提到的,我有220万条记录,这就是为什么查询需要永久执行。 –

+0

第一个查询应该在Hive上工作,因为Hive不支持它。支持子查询,也支持coalesce()和max()。请尝试运行它,它应该很好 – leftjoin

+0

这个'--pivot table2'只是一个注释。 – leftjoin