在Impala中按组减去最大,最小日期

问题描述 投票:0回答:1

我有一个查询,我在impala中找到用户的最大和最小日期:

select max(id_date) as last_tran
       ,min(id_date) as first_tran
       ,user
from table_1 a 
join table_2 d
on a.id = d.id
group by 3

我想再按用户减去最小和最大日期。

在Impala中我尝试使用date_sub函数,但它不起作用。

select date_sub(last_tran, first_tran) as date_len
, user
from
(select max(id_date) as last_tran
       ,min(id_date) as first_tran
       ,user
from table_1 a 
join table_2 d
on a.id= d.id
group by 3) time
group by 1,2

似乎date_sub函数的第二个参数必须是一个表示天数的整数。

我怎么能绕过这个?

hadoop impala
1个回答
0
投票

为了使这项工作,datediff函数应该像这样使用:

select datediff(last_tran, first_tran) as date_len
, user
from
(select max(id_date) as last_tran
       ,min(id_date) as first_tran
       ,user
from table_1 a 
join table_2 d
on a.id= d.id
group by 3) time
group by 1,2

该函数返回开始日期和结束日期之间的天数。从documentation看来,天的回报值似乎无法改变。

© www.soinside.com 2019 - 2024. All rights reserved.