案例一:自然周内最大活跃天数,按“日历周(周一到周日)”统计。思路:①给每一天标上年 + 周号②按 (gaid, 年, 周)统计活跃天数③再按 gaid 取最大值易错点:①周号的写法②统计活跃天数时要去重selectgaid,max(active_days) as max_active_days_in_weekfrom (selectgaid,year(ds) as yr,weekofyear(ds) as wk,count(distinct ds) as active_daysfrom active_loggroup by gaid, year(ds), weekofyear(ds)) tgroup by gaid;案例二:任意连续 7 天内最大活跃天数(滑动窗口)思路:① 以每一天作为窗口终点对每一个 ds,看它往前 6 天 + 当天② 统计该窗口内的活跃天数用 count(distinct ds),避免同一天多次活跃③ 按用户取最大值得到每个用户在任意连续 7 天内,最多活跃几天易错点:①窗口写错成 ROWS,range between 6 preceding按日期值②统计活跃天数时要去重count(distinct ds)selectgaid,max(window_active_days) as max_active_days_last_7dfrom (selectgaid,ds,count(*) over (partition by gaidorder by dsrange between 6 preceding and current row) as window_active_daysfrom active_log) tgroup by gaid;