【发布时间】:2021-02-21 19:02:35
【问题描述】:
使用 MySQL,我试图获取我在任何给定月份的活跃用户数。我有一个包含ActivationDate 和TerminationDate 列的表,如果计算的月份在ActivationDate 和TerminationDate 之后为空,则用户处于活动状态并且应该被计算在内。我想按月总结这些金额。我想我可以只sum 每边计算总数,但分解它不会给我一个运行总数。我尝试过使用窗口函数,但我没有足够的经验来确切地知道我做错了什么,而且我不确定如何提出正确的问题。
例如,如果我有以下数据...
UserId ActivationDate TerminationDate
1 2020-01-01 null
2 2020-01-15 null
3 2020-01-20 2020-01-30
4 2020-02-01 null
5 2020-02-14 2020-02-27
6 2020-02-15 2020-02-28
7 2020-03-02 null
8 2020-03-05 null
9 2020-03-20 2020-03-21
我希望我的结果类似于:
2020-01 2 (there are 2 active users, since one signed up but cancelled before the end of the month)
2020-02 3 (2 from the previous month, plus 1 that signed up this month and is still active)
2020-03 5 (3 from previous, 2 new, 1 cancellation)
【问题讨论】:
标签: mysql sql datetime count window-functions