ログインテーブル(login_log)から、日毎の累計ログインUU数を月毎に計算したいです。
以下のように、日毎の累計ログインUU数を月をまたいで計算することはできるのですが、月初でUU数をリセットする方法が分かりません。
MySQL
1select date_format(created_at,"%Y-%m-%d") as login_date, 2(select count(distinct(user_id)) 3from login_log where date_format(created_at,"%Y-%m-%d") <= date_format(LL.created_at,"%Y-%m-%d")) total_uu 4from login_log AS LL 5group by date_format(created_at,"%Y-%m-%d")
例えば下のようなテーブルがあったときに
| user_id | created_at |
|---|---|
| 1 | 2022-01-29 10:33:05 |
| 2 | 2022-01-31 10:33:05 |
| 2 | 2022-02-01 10:33:05 |
| 1 | 2022-02-04 10:33:05 |
| 3 | 2022-02-05 10:33:05 |
上記のクエリだと以下の結果になりますが、
| login_date | total_uu |
|---|---|
| 2022-01-29 | 1 |
| 2022-01-31 | 2 |
| 2022-02-05 | 3 |
求めたいのは以下の結果です。
| login_date | total_uu |
|---|---|
| 2022-01-29 | 1 |
| 2022-01-31 | 2 |
| 2022-02-01 | 1 |
| 2022-02-04 | 2 |
| 2022-02-05 | 3 |
ご教示いただけますと幸いです。
回答1件
あなたの回答
tips
プレビュー
2022/06/14 03:34