I have two table like below in 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 |
+-----+---------+------------------------+
From table A
I want to find the id
, c_name
, c_date
from record which has occured before test_time
in table B
for each id
based on 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 |
+-----+-----------+------------------------+
I have tried like below But not getting correct result`
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
How can I get the expected result