我在HIVE
中有两个表格,如下所示。
Table A
+-----+---------+-----------+------------------------+------------------------+
| id | event | c_name | c_date | test_time |
+-----+---------+-----------+------------------------+------------------------+
| 1 | click | abc | 2018-07-02 22:36:32.0 | 2018-06-22 22:36:32.0 |
| 2 | click | abc 123 | 2018-07-01 22:36:32.0 | 2018-06-01 22:36:32.0 |
| 2 | click | abc | 2018-07-02 23:46:32.0 | 2018-07-02 23:46:32.0 |
| 3 | done | abc 345 | 2018-07-22 23:56:32.0 | 2018-07-22 22:36:32.0 |
| 4 | done | 123 abc | 2018-08-22 22:36:32.0 | 2018-08-12 22:36:32.0 |
| 1 | click | abc 123 | 2018-07-01 22:36:32.0 | 2018-07-01 22:36:32.0 |
+-----+---------+-----------+------------------------+------------------------+
Table B
+-----+---------+------------------------+
| id | event | test_time |
+-----+---------+------------------------+
| 1 | signup | 2018-07-01 20:36:32.0 |
| 2 | signup | 2018-07-02 23:36:32.0 |
| 3 | signup | 2018-08-02 20:36:32.0 |
| 4 | signup | 2018-09-02 20:36:32.0 |
+-----+---------+------------------------+
从table A
中,我想从id
中c_name
之前的c_date
,test_time
,table B
中查找每个id
基于test_time
。
Expected result
+-----+-----------+------------------------+
| id | c_name | c_date |
+-----+-----------+------------------------+
| 1 | abc | 2018-07-02 22:36:32.0 |
| 2 | abc 123 | 2018-07-01 22:36:32.0 |
| 3 | abc 345 | 2018-07-22 23:56:32.0 |
| 4 | 123 abc | 2018-08-22 22:36:32.0 |
+-----+-----------+------------------------+
我已经尝试过以下方法,但没有得到正确的结果。
select a.c_name, a.c_date, b.id from table A a left outer join table B b where a.id = b.id and a.test_time < b.test_time
如何获得预期的结果
答案 0 :(得分:1)
使用LEFT JOIN
,请尝试以下操作:
SELECT ta.id,
ta.c_name,
ta.c_date
FROM Table_A AS ta
LEFT JOIN Table_B AS tb
ON tb.id = ta.id
WHERE tb.test_time > ta.test_time