生成查询以加入不同的记录

时间:2016-07-19 20:51:26

标签: mysql hive

sql / hive中的新手......

 SELECT id,seed, day FROM  table_1 WHERE  day = to_date('2016-06-09') limit 5;
302766500   R388899 2016-06-09
692010468   R61140  2016-06-09
662084962   R165803 2016-06-09
1818260515  R411276 2016-06-09
646246322   R426737 2016-06-09


SELECT id, exp, day FROM table_2 WHERE day = to_date('2016-06-09')  limit 5;
OK
2595    e137_1  2016-06-09
2595    e137_2  2016-06-09
4372    e137_1  2016-06-09
7256    e137_1  2016-06-09
18674   e137_1  2016-06-09
Time taken: 1.475 seconds, Fetched: 5 row(s)

正如您在表2中所见..重复监听者ID(2595)..

因此,从表2中,我想找出两天范围之间的不同ID

然后在给定的日期范围内找到表1中这些ID的种子。

SELECT id, seed FROM table_1 WHERE day = to_date('some date')
JOIN (
SELECT DISTINCT id FROM table_2 WHERE day = to_date('some_date')
) l
ON l.id = id;

但是我收到了这个错误:

ParseException line 3:0 missing EOF at 'JOIN' near ')'

2 个答案:

答案 0 :(得分:2)

似乎位置错误

SELECT table_1.id, table_1.seed FROM table_1 
JOIN (
SELECT DISTINCT id FROM table_2 WHERE day = to_date('some_date')
) l
ON l.id = table_1.id
WHERE table_1.day = to_date('some date')

;

答案 1 :(得分:1)

始终加入from部分:

SELECT id, seed FROM table_1 JOIN (
SELECT DISTINCT id FROM table_2 WHERE day = to_date('some_date')
) l
ON l.id = table_1.id
WHERE day = to_date('some date')