ハイブの分析機能とタイムスタンプの使用

サンプルデータは次のとおりです。フィールドcreateTimeとmemberIdのみを表示します。

createTime                    memberId
2017/11/13 2017-11-13 12:00:01 8a9e7bf05d7ec61b015d89e060901ef8
2017/11/13 2017-11-13 12:01:01 8a9f156c5d409b7d015d4566b0f06b06
2017/11/13 2017-11-13 12:02:01 8a9f17655e078b0e015e14a79de02059


要件はどのようにして同じmemberIdを間隔を置いて表示しますか?


---テストテーブルを作成します

create table member_create(memberId String,creatime TIMESTAMP);


-データを挿入します

insert into member_create values('8a9e7bf05d7ec61b015d89e060901ef8','2017-11-13 12:00:01');
insert into member_create values('8a9e7bf05d7ec61b015d89e060901ef8','2017-11-13 12:01:01');
insert into member_create values('8a9e7bf05d7ec61b015d89e060901ef8','2017-11-13 12:02:01');


---テストステートメント

select memberid, creatime, last_creatime,
case when last_creatime is null then 0 else unix_timestamp(creatime)-unix_timestamp(last_creatime) end diff_time
 from (select memberid, creatime, lag(creatime) over(partition by memberid order by creatime) last_creatime
from member_create) t;

-の結果

8a9e7bf05d7ec61b015d89e060901ef8 2017-11-13 12:00:01 NULL 0
8a9e7bf05d7ec61b015d89e060901ef8 2017-11-13 12:01:01 2017-11-13 12:00:01 60
8a9e7bf05d7ec61b015d89e060901ef8 2017-11-13 12:02:01 2017-11-13 12:01:01 60


説明:オフセット関数の使用法をリストしました。個人的には、ハイブの分析関数はオラクルの分析関数に似ていると思います。

おすすめ

転載: blog.csdn.net/zhaoxiangchong/article/details/78550231