1. 程式人生 > >hive sql 時間遞迴寫法

hive sql 時間遞迴寫法

select a.p_day,0 fuv,0 vv,0 as fuv_sec,0 as vv_sec,count(distinct b.uid) fuv_sev,sum(b.vv) vv_sev
from
(select p_day,uid,sum(vv)vv
from default.t_1
where p_day>=20180706 and p_day<=20180719
and pro =1 and channeled in (1000130000,1000130024)
and vv>0
group by p_day,uid)a
left outer join
(select p_day,uid,sum(vv)vv
from default.t_2
where p_day>=20180706 and p_day<=20180719
and pro =1 and channeled in (1000130000,1000130024) 
and vv>0
group  by p_day,uid)b 
on a.uid=b.uid
where datediff( to_date( from_unixtime(to_unix_timestamp(cast(b.p_day as string),'yyyyMMdd'),'yyyy-MM-dd')),to_date( from_unixtime(to_unix_timestamp(cast(a.p_day as string),'yyyyMMdd'),'yyyy-MM-dd')))=6
group by a.p_day

上面的前提是右表左表不為空

可以使用

on date_add(to_date( from_unixtime(to_unix_timestamp(cast(b.p_day as string),'yyyyMMdd'),'yyyy-MM-dd')),6)=to_date( from_unixtime(to_unix_timestamp(cast(a.p_day as string),'yyyyMMdd'),'yyyy-MM-dd'))

select t1.uid,t2.p_day  from
(select  uid,p_day from default.t_app_free_action 
where p_day >=20180702 and p_day<=20180715  and pro =1 and newuser=1 group by uid,p_day)t1
left join (select  uid,p_day from default.t_app_free_action 
where p_day >=20180801 and p_day<=20180814  and pro =1 group by uid,p_day)t2
on t1.uid=t2.uid and date_add(to_date(from_unixtime(to_unix_timestamp(cast(t1.p_day as string),'yyyyMMdd'),'yyyy-MM-dd')),30)=to_date( from_unixtime(to_unix_timestamp(cast(t2.p_day as string),'yyyyMMdd'),'yyyy-MM-dd'))
having t2.p_day is   null limit 10