一、现象
在跑数据的时候,会因为dt查询时间范围的限制,比如一个数据库表很大,dt最多只能查询最近三个月的数据,这个时候找到该用户在app首次访问时间,就会出现困难。
用两步即可解决:
第一步,从app埋点表【app_table】取dt第一天,呈现用户号及访问时间,生成表名为app首次访问时间表【英文名为“app_first_view_table”】,为后续取前一天与app埋点表取dt当天做比较。
第二步,用app_first_view_table的前一天,跟app埋点表的今天,做个union all。用户号做聚合,访问时间获取最小值即可。
二、解决方案
第一步:
create table app_first_view_table as
select user_id
, view_time as first_time
from app_table
where dt = '20240101' # '$date'
第二步:
select user_id
, min(first_time) as first_time
from
(
select user_id
, first_time
from app_first_view_table
where dt = '20240101' # '$date - 1'
union all
select user_id
, view_time as first_time
from app_table
where dt = '20240102' # '$date'
) t
group by user_id
备注:在工业界中,其实使用很便利,有系统每日自动执行任务,时间参数可以获取当天,比如’$date’,具体参数名称由企业IT而定哈,这里只是举个时间参数名称例子,方便大家理解