回答編集履歴

2

変更

2020/06/16 16:44

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -1,3 +1,9 @@
1
+ > 1月に課金(charge_logのcategoryがbuy)したユーザーについて、
2
+
3
+ > ユーザーごとに、ユーザーID・2月のログイン回数・2月にクエストを行った日数・2月にクエストを行った回数を出力したい。
4
+
5
+
6
+
1
7
  複数のテーブルで集計する際には、集計する単位で揃える必要があります。
2
8
 
3
9
  ```SQL

1

質問に合わせる

2020/06/16 16:44

投稿

sazi
sazi

スコア25199

test CHANGED
@@ -5,8 +5,6 @@
5
5
  select login.user_id
6
6
 
7
7
  , login.cnt as login_count
8
-
9
- , coalesce(charge.cnt, 0) as charge_count
10
8
 
11
9
  , coalesce(quest.day_count, 0) as quest_day_count
12
10
 
@@ -18,7 +16,17 @@
18
16
 
19
17
  from login_log
20
18
 
21
- where MONTH(timestamp) = 5
19
+ where MONTH(timestamp) = 2
20
+
21
+ and user_id in (
22
+
23
+ select user_id
24
+
25
+ from charge_log
26
+
27
+ where MONTH(timestamp) = 1 and category = 'buy'
28
+
29
+ )
22
30
 
23
31
  group by user_id
24
32
 
@@ -26,25 +34,11 @@
26
34
 
27
35
  left join (
28
36
 
29
- select user_id, count(*) as cnt
30
-
31
- from charge_log
32
-
33
- where MONTH(timestamp) = 5 and category = 'buy'
34
-
35
- group by user_id
36
-
37
- ) charge
38
-
39
- on login.user_id=charge.user_id
40
-
41
- left join (
42
-
43
37
  select user_id, count(*) as cnt, count(distinct date(timestamp)) as day_count
44
38
 
45
39
  from quest_log
46
40
 
47
- where MONTH(timestamp) = 5
41
+ where MONTH(timestamp) = 2
48
42
 
49
43
  group by user_id
50
44