使用 Impala 获取连续行程的计数
Using Impala get the count of consecutive trips
示例数据
touristid|day
ABC|1
ABC|1
ABC|2
ABC|4
ABC|5
ABC|6
ABC|8
ABC|10
输出应该是
touristid|trip
ABC|4
4 背后的逻辑是连续天数不同的连续天数 sqq 1,1,2 是第 1,然后 4,5,6 是第 2,然后 8 是第 3,10 是第 4
我想要这个输出使用 impala query
使用 lag() 函数获取前一天,计算 new_trip_flag 如果 day-prev_day>1,则计数(new_trip_flag)。
演示:
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;
结果:
touristid trip_cnt
ABC 4
同样适用于 Hive。
示例数据
touristid|day
ABC|1
ABC|1
ABC|2
ABC|4
ABC|5
ABC|6
ABC|8
ABC|10
输出应该是
touristid|trip
ABC|4
4 背后的逻辑是连续天数不同的连续天数 sqq 1,1,2 是第 1,然后 4,5,6 是第 2,然后 8 是第 3,10 是第 4 我想要这个输出使用 impala query
使用 lag() 函数获取前一天,计算 new_trip_flag 如果 day-prev_day>1,则计数(new_trip_flag)。
演示:
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;
结果:
touristid trip_cnt
ABC 4
同样适用于 Hive。