teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

変更

2020/06/16 16:44

投稿

sazi
sazi

スコア25430

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

1

質問に合わせる

2020/06/16 16:44

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -2,26 +2,23 @@
2
2
  ```SQL
3
3
  select login.user_id
4
4
  , login.cnt as login_count
5
- , coalesce(charge.cnt, 0) as charge_count
6
5
  , coalesce(quest.day_count, 0) as quest_day_count
7
6
  , coalesce(quest.cnt, 0) as quest_count
8
7
  from (
9
8
  select user_id, count(*) as cnt
10
9
  from login_log
11
- where MONTH(timestamp) = 5
10
+ where MONTH(timestamp) = 2
11
+ and user_id in (
12
+ select user_id
13
+ from charge_log
14
+ where MONTH(timestamp) = 1 and category = 'buy'
15
+ )
12
16
  group by user_id
13
17
  ) login
14
18
  left join (
15
- select user_id, count(*) as cnt
16
- from charge_log
17
- where MONTH(timestamp) = 5 and category = 'buy'
18
- group by user_id
19
- ) charge
20
- on login.user_id=charge.user_id
21
- left join (
22
19
  select user_id, count(*) as cnt, count(distinct date(timestamp)) as day_count
23
20
  from quest_log
24
- where MONTH(timestamp) = 5
21
+ where MONTH(timestamp) = 2
25
22
  group by user_id
26
23
  ) quest
27
24
  on login.user_id=quest.user_id