获取 Hive 中最近 7 天的记录

Use*_*345 2 hive hiveql

hive我有一张如下所示的表格。我想从这个表中insertdate选择在哪里。customer_idinsertdatecurrent_date - 7 days

original table

+------------------------+--------------+
|       insertdate       | customer_id  |
+------------------------+--------------+
| 2018-04-21 04:00:00.0  | 39550695     |
| 2018-04-22 04:00:00.0  | 38841612     |
| 2018-04-23 03:59:00.0  | 23100419     |
| 2018-04-24 03:58:00.0  | 39550688     |
| 2018-04-25 03:58:00.0  | 39550691     |
| 2018-05-12 03:57:00.0  | 39550685     |
| 2018-05-13 03:57:00.0  | 39550687     |
| 2018-05-14 03:57:00.0  | 39550677     |
| 2018-05-14 03:56:00.0  | 30254216     |
| 2018-05-14 03:56:00.0  | 39550668     |
+------------------------+--------------+
Run Code Online (Sandbox Code Playgroud)

expected result

+------------------------+--------------+
|       insertdate       | customer_id  |
+------------------------+--------------+
| 2018-05-12 03:57:00.0  | 39550685     |
| 2018-05-13 03:57:00.0  | 39550687     |
| 2018-05-14 03:57:00.0  | 39550677     |
| 2018-05-14 03:56:00.0  | 30254216     |
| 2018-05-14 03:56:00.0  | 39550668     |
+------------------------+--------------+
Run Code Online (Sandbox Code Playgroud)

但当我尝试以下操作时,我得到空结果

select insert_date, customer_id from table where insert_date = date_sub(current_date, 7);

select insert_date, customer_id from table whereinsert_date = date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'), 7);
Run Code Online (Sandbox Code Playgroud)

对于上述两个查询,我都得到空结果。

我在这里做错了什么以及如何获得正确的结果?

Vam*_*ala 5

假设您需要过去 7 天的数据,请使用

select insert_date, customer_id 
from table 
where to_date(insert_date) >= date_sub(current_date, 7) 
and to_date(insert_date) < current_date
Run Code Online (Sandbox Code Playgroud)

您的查询不显示结果的原因是日期时间格式和日期之间的比较。