12个月的滑动窗口统计不同用户的数量

问题描述 投票:0回答:1

我有一个相当基本的数据集,其中有一个表,包含了用户每次与应用互动的时间戳。一个活跃的用户被归类为在过去12个月内至少与应用互动过一次的人。

我需要制作一个表,逐日告诉我在之前的12个月期间有多少 "活跃 "用户(可以追溯到n天前)。我需要在Amazon Athena中运行查询。

一个可能的复杂性是,一个用户每天都可能与应用程序进行交互。我想知道什么是最好的窗口函数可以捕捉到这一点。

数据的格式是这样的。

A   Opened App  10/04/2020
A   Opened App  10/02/2020
A   Opened App  05/01/2020
B   Opened App  12/03/2020
B   Opened App  02/01/2019
B   Opened App  20/07/2018
C   Opened App  19/04/2019

我需要一个结果表

20/04/2020  2 (A and B)
19/04/2020  2 (A and B)
18/04/2020  3 (all three)
... 
04/01/2020  1 (Only C)
... 
sql count window amazon-athena sliding-window
1个回答
0
投票

一种方法是使用 count(distinct) 附带 range 窗口函数。

select distinct date,
       count(distinct user) over (order by date range between interval '1 year' preceding and current row) as num_active_users
from t;

不是所有的数据库都支持这种语法。

© www.soinside.com 2019 - 2024. All rights reserved.