hive上連續登入天數的查詢

2021-07-30 06:29:58 字數 3562 閱讀 1594

1、資料測試表及測試資料

測試表表結構:

hive> desc data_room;

okroomid string

pt_month string

pt_day string

# partition information

# col_name data_type comment

pt_month string

pt_day string

time taken: 0.158 seconds, fetched: 9 row(s)

查詢測試資料:

select * from data_room where pt_day between '2017-01-01' and '2017-01-16' and roomid=9999589;
9999589 2017-01 2017-01-01

9999589 2017-01 2017-01-02

9999589 2017-01 2017-01-04

9999589 2017-01 2017-01-05

9999589 2017-01 2017-01-06

9999589 2017-01 2017-01-07

9999589 2017-01 2017-01-08

9999589 2017-01 2017-01-09

9999589 2017-01 2017-01-10

9999589 2017-01 2017-01-12

9999589 2017-01 2017-01-13

9999589 2017-01 2017-01-14

9999589 2017-01 2017-01-15

9999589 2017-01 2017-01-16

2、實現連續登入天資料查詢的sql

實現的sql:

with tab_room_live_continuity_preproc as(

select roomid,compare_day,continuity_days,sum(continuity_days)over(partition by roomid order by compare_day asc rows between unbounded preceding and current row) cumulative_continuity_days,row_number()over(partition by roomid order by compare_day asc) rn

from (

select roomid,date_sub(pt_day,rn) compare_day,count(*) continuity_days

from (select roomid,to_date(pt_day) pt_day,row_number()over(partition by roomid order by to_date(pt_day) asc) rn

from data_room

where roomid=9999589 and pt_day between '2017-01-01' and '2017-01-16') x

group by roomid,date_sub(pt_day,rn)) xx)

select roomid,continuity_days,continuity_frist_day,continuity_last_day from (

select roomid,compare_day,continuity_days,cumulative_continuity_days,date_add(compare_day,int(cumulative_continuity_days)-int(continuity_days)+1) continuity_frist_day,date_add(compare_day,int(cumulative_continuity_days)) continuity_last_day

from tab_room_live_continuity_preproc) x;

查詢結果:

9999589 2       2017-01-01      2017-01-02

9999589 7 2017-01-04 2017-01-10

9999589 5 2017-01-12 2017-01-16

3、說明與總結

此處用到了分析函式,如計算累積值、排名函式等,再構造sql的過程中,也嘗試使用了位移函式lag/lead和層次查詢。總之,初看起來還是比較複雜的,一時半會不一定就能解決的了。

這些函式及sql**是在hadoop/hive平台上進行測試與驗證的,相信在oracle上實現應該也沒有太大的問題;但相對於較低端開源免費的mysql上,如實現起來可能會相當麻煩,目前mysql對分析函式還不支援。

補:其實還可以更簡單

select roomid,min(pt_day) continuity_frist_day,max(pt_day) continuity_last_day,count(*) continuity_days

from (select roomid,to_date(pt_day) pt_day,row_number()over(partition by roomid order by to_date(pt_day) asc) rn

from data_room

where roomid=9999589 and pt_day between '2017-01-01' and '2017-01-16') x

group by roomid,date_sub(pt_day,rn);

查詢結果:

9999589 2017-01-01      2017-01-02      2

9999589 2017-01-04 2017-01-10 7

9999589 2017-01-12 2017-01-16 5

Hive sql 查詢連續登入天數

目前有兩列資料,分別是使用者id和使用者登入的時間,現需要統計使用者連續登入的最大天數,中間如有斷開,則不算連續,如下圖示例。在hive中進行操作,首先啟動hadoop集群環境,進入到hadoop安裝目錄,sbin start dfs.sh,jps檢視hadoop集群有無正常啟動 正常啟動後,輸入h...

連續登入天數計算

最近有個需求,計算使用者連續登入的最大天數 這裡使用prestosql,使用hive也可以 先看下登入日誌資料表hive.traffic.access user只有兩個字段 uid,day 日期輔助表hive.ods.dim date,這個表只有乙個欄位day 先說下思路,uidday rownum...

查詢淨值連續增長天數

sql查詢連續增長天數 這裡寫了乙個查詢淨值連續增長天數的方法,跟大家分享 表結構示例 表t hangqingbiao,存放 產品號,日期,累計淨值 三個字段 查詢每個產品號的累計淨值連續增長的天數 with tmp grow as select a.c fundcode,a.d date,case...