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

时间:2018-03-01 13:41:22

标签: hadoop impala

我有一个查询,我在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函数的第二个参数必须是表示天数的整数。

我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分: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开始,似乎无法更改天数的返回值。