Using Impala get the count of consecutive trips

喜你入骨 提交于 2019-12-08 18:42:34

Get previous day using lag() function, calculate new_trip_flag if the day-prev_day>1, then count(new_trip_flag).

Demo:

with table1 as (
select 'ABC' as touristid, 1  as day union all
select 'ABC' as touristid, 1  as day union all
select 'ABC' as touristid, 2  as day union all
select 'ABC' as touristid, 4  as day union all
select 'ABC' as touristid, 5  as day union all
select 'ABC' as touristid, 6  as day union all
select 'ABC' as touristid, 8  as day union all
select 'ABC' as touristid, 10 as day 
)

select touristid, count(new_trip_flag) trip_cnt
  from 
       ( -- calculate new_trip_flag
         select touristid,
                case when (day-prev_day) > 1 or prev_day is NULL then true end  new_trip_flag
           from       
                ( -- get prev_day
                  select touristid, day, 
                         lag(day) over(partition by touristid order by day) prev_day
                    from table1
                )s
        )s
 group by touristid;

Result:

touristid   trip_cnt    
ABC         4   

The same will work in Hive also.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!