使用Hive获取7天的数据

时间:2017-03-20 15:32:56

标签: hive

我有一张不同日期的表

2014-11-20
2014-12-12
2014-11-10
2014-12-13
2014-10-12
2016-01-15
2016-01-14
2016-01-16
2016-01-18

想要最近7天的数据(最大日期-7)

2016-01-15
2016-01-14
2016-01-16
2016-01-18

我尝试了下面的查询,但没有工作

select * from date_txt1 where DATEDIFF((select max(purchase_date) from date_txt1),(select min(purchase_date) from date_txt1)) <= 7;

2 个答案:

答案 0 :(得分:0)

在&#34; datediff&#34;作为第二个参数只使用列名:

select * from date_txt1 
where DATEDIFF((select max(purchase_date) from date_txt1), purchase_date) <= 7;

更新:&#34; max&#34;提取:

with maxDate as (
    select max(purchase_date)  as end_date
    from date_txt1
)
Select * 
From date_txt1 , maxDate
where DATEDIFF(maxDate.end_date, purchase_date) <= 7;

答案 1 :(得分:0)

select a.purchase_date 
from date_txt1 a
cross join ( select max( purchase_date ) as maxdate from date_txt1 ) b
where DATEDIFF( b.maxdate, a.purchase_date ) <= 7