我有一个查询,我在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
函数的第二个参数必须是表示天数的整数。
我怎样才能解决这个问题?
答案 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开始,似乎无法更改天数的返回值。