回答編集履歴

3

ちょうせい

2019/11/21 00:42

投稿

yambejp
yambejp

スコア116661

test CHANGED
@@ -71,3 +71,51 @@
71
71
  |10|1|1|0|
72
72
 
73
73
  |20|1|0|0|
74
+
75
+
76
+
77
+ # true/false
78
+
79
+ カウントしていた結果を0より大きいかどうかで判断してはどうでしょうか?
80
+
81
+ ```SQL
82
+
83
+ SELECT
84
+
85
+ user_id,
86
+
87
+ SUM(CASE SUBSTRING(created_at, 1, 10)
88
+
89
+ WHEN '2019-11-01' THEN 1
90
+
91
+ ELSE 0
92
+
93
+ END)>0 AS one_day,
94
+
95
+ SUM(CASE SUBSTRING(created_at, 1, 10)
96
+
97
+ WHEN '2019-11-02' THEN 1
98
+
99
+ ELSE 0
100
+
101
+ END)>0 AS two_day,
102
+
103
+ SUM(CASE SUBSTRING(created_at, 1, 10)
104
+
105
+ WHEN '2019-11-03' THEN 1
106
+
107
+ ELSE 0
108
+
109
+ END)>0 AS three_day
110
+
111
+ FROM
112
+
113
+ posts
114
+
115
+ WHERE
116
+
117
+ DATE(SUBSTRING(created_at,1,10)) BETWEEN DATE('2019-11-01') AND DATE('2019-11-03')
118
+
119
+ GROUP BY user_id
120
+
121
+ ```

2

けっか

2019/11/21 00:42

投稿

yambejp
yambejp

スコア116661

test CHANGED
@@ -59,3 +59,15 @@
59
59
  GROUP BY user_id
60
60
 
61
61
  ```
62
+
63
+ - 結果
64
+
65
+
66
+
67
+ |user_id|one_day|two_day|three_day|
68
+
69
+ |--:|--:|--:|--:|
70
+
71
+ |10|1|1|0|
72
+
73
+ |20|1|0|0|

1

ちょうせい

2019/11/20 10:59

投稿

yambejp
yambejp

スコア116661

test CHANGED
@@ -1,4 +1,22 @@
1
1
  一応こんな感じでいけるかもしれません
2
+
3
+ - 元データ
4
+
5
+ ```SQL
6
+
7
+ create table posts(id int,user_id int,created_at varchar(30));
8
+
9
+ insert into posts values
10
+
11
+ (1,10,'2019-11-01T22:11:05.000+09:00'),
12
+
13
+ (2,10,'2019-11-02T22:11:05.000+09:00'),
14
+
15
+ (3,20,'2019-11-01T22:11:05.000+09:00');
16
+
17
+ ```
18
+
19
+ - 検索
2
20
 
3
21
  ```SQL
4
22