获取Hive过去7天的记录

时间:2018-05-15 18:09:20

标签: hive hiveql

我在hive中有一张表,如下所示。我想从insertdatecustomer_id的表格中选择insertdatecurrent_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     |
+------------------------+--------------+

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     |
+------------------------+--------------+

但是当我尝试以下

时,我的结果是空的
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);

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

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

1 个答案:

答案 0 :(得分:2)

假设您需要过去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

您的查询未显示结果的原因是由于日期时间格式与日期之间的比较。