【问题标题】:Sliding window of count distinct users for 12 months统计不同用户的滑动窗口 12 个月
【发布时间】:2020-04-29 17:30:59
【问题描述】:

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

我需要制作一个表格,它会告诉我每天(回溯 n 天)在前 12 个月期间有多少“活跃”用户。我需要在 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)
... 

【问题讨论】:

  • 请用您正在使用的数据库标记您的问题:mysql、oracle、sql-server...?

标签: sql count window amazon-athena sliding-window


【解决方案1】:

一种方法是使用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;

并非所有数据库都支持这种语法。

【讨论】:

  • 谢谢。你知道 Athena 的语法是什么吗?我目前正在尝试;从表中选择不同的日期(时间戳),计数(不同的 id)(按日期(时间戳)范围在间隔“1”年前和当前行之间的顺序)作为 num_active_users;收到错误窗口框架起始值类型必须是 INTEGER 或 BIGINT(实际间隔年到月)
  • @史蒂夫。 . .我没有直接访问 Athena 的权限,并且很难弄清楚正确的语法是什么,因为它似乎使用的是旧版本的 Presto。你知道哪里有专门关于 Athena 的好文档吗?
  • 这里是 Amazon Athena 的 SQL 参考:SQL Reference for Amazon Athena
  • @JohnRotenstein 。 . .这就是函数引用的地方:docs.aws.amazon.com/athena/latest/ug/…。 Presto 现在远远超出了那个版本。
猜你喜欢
  • 2019-10-03
  • 2020-10-10
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 2019-01-04
  • 2018-05-11
相关资源
最近更新 更多