select distinct a.phone,from_unixtime(cast(floor(a.ts/1000) as bigint),'yyyyMMdd HH:mm:ss'),cha
from table a
join
(
select a.phone,a.ts,abs(a.ts-b.ts)/1000 cha
from (select phone,ts,row_number() over(partition by phone order by ts ) rank from table) a
left join
( select phone,ts,rank-1 as rank from (select phone,ts,row_number() over(partition by phone order by ts ) rank from table) a ) b
on a.phone = b.phone and a.rank = b.rank
) b
on a.phone = b.phone and a.ts = b.ts
where a.phone is not null and a.phone>'';