使用hive获取7天数据

p4rjhz4m  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(1642)

我有一张不同日期的table

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;
ogq8wdun

ogq8wdun1#

在“datediff”中,作为第二个参数,只使用列名:

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

更新:“max”提取:

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;
bttbmeg0

bttbmeg02#

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

相关问题