【问题标题】:rolling count on Athena or Quicksight在 Athena 或 Quicksight 上滚动计数
【发布时间】:2021-02-22 06:36:32
【问题描述】:

我有这个数据集

date         id
1/1/2020     1
1/1/2020     2
...
n            m

我想滚动统计 AWS Quicksight 或 Athena 上不同的月度用户。例如

date         MAU
1/1/2020     -
1/2/2020     -
1/30/2020    100
1/31/2020    102

100 on 1/30/2020 表示在过去 30 天内,有 100 个不同的用户处于活动状态(从 2020 年 1 月 1 日到 2020 年 1 月 30 日)。 102 on 1/31/2020 表示在过去 30 天内有 102 个不同的用户处于活动状态(从 2020 年 1 月 2 日到 2020 年 1 月 30 日)

【问题讨论】:

    标签: sql datetime count presto amazon-athena


    【解决方案1】:

    基本思想是使用带有范围框架的窗口计数。如果我们将日期转换为纪元并使用以下范围框架,它在 Amazon Athena 中是否有效?

    select date, 
        sum(count(*)) over(
            order by to_unixtime(date)
            range between - 60 * 60 * 24 * 30 preceding and current row
        ) mau
    from mytable
    group by date
    

    窗口函数解决方案的替代方案是相关子查询:

    select date, 
        count(*) + (
            select count(*) 
            from mytable t1
            where t1.date >= t.date - interval '30' day and t1.date < t.date
        ) mau
    from mytable
    group by date
    

    【讨论】:

    • 是按 id 区分计数吗?
    猜你喜欢
    • 1970-01-01
    • 2021-06-07
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多