蜂巢:联接表

时间:2018-08-15 18:02:03

标签: sql hive

我刚刚开始学习SQL和Hive。在尝试完成面临问题的任务时。

有两个表:

db_base.memberdata tb_1
    
tb_1.mem_id,
tb_1.mem_nbr,
tb_1.mem_grp,
tb_1.mem_ctry,
tb_1.mem_email, 
tb_1.mem_enrldate



db_history.memberhdata tb_2 
tb_2.mem_id,
tb_2.mem_nbr,
tb_2.mem_grp,
tb_2.mem_stat,
tb_2.mem_cd,
tb_2.mem_lvl,
tb_2.mem__stat_desc,
tb_2.mem_enrldate

尝试通过以下方式加入表格:

SELECT tb_1.mem_id,tb_1.mem_nbr,tb_1.mem_grp,tb_1.mem_ctry,tb_1.mem_email,tb_1.mem_enrldate 
    FROM db_base.memberdata tb_1
INNER JOIN
(SELECT 
tb_2.mem_id,tb_2.mem_nbr,tb_2.mem_grp,tb_2.mem_stat,tb_2.mem_cd,tb_2.mem_lvl,tb_2.mem__stat_desc,
tb_2.mem_enrldate
FROM db_history.memberhdata tb_2 
WHERE 
tb_2.mem_enrldate BETWEEN '2018-01-01' AND '2018-06-01' 
AND tb_2.mem_id = 4 AND tb_2.mem_nbr = 1 
AND tb_2.mem_grp = 1) tb_2a
ON tb_2a.mem_id = tb_1.mem_id 
limit 100;

我遇到的错误是:

FAILED: ParseException line 9:0 cannot recognize input near 'ON' 'tb_2' '.' in subquery source 

试图在线查看几个示例,但似乎没有任何效果。抱歉,我的知识有限。请帮忙。

在查询中进行更新后,得到:

FAILED: SemanticException [Error 10002]: Line 13:9 Invalid column reference 'mem_id'

1 个答案:

答案 0 :(得分:0)

我注意到您正在从每个表中选择所有内容,因此,为简单起见,我只是将其简短表示(通过使用*)

SELECT * FROM tb_1 as AA
INNER JOIN
(
select * from tb_2 as Tb_2
WHERE
Tb_2.enrldate between  months_between('2018-06-01', '2015-01-01')
AND Tb_2.id=4 AND Tb_2.nbr=1
AND tb_2.grp=1
) as BB
ON AA.id=BB.id
LIMIT 100;

请注意别名AA和BB的位置。