回答編集履歴
3
更新
test
CHANGED
@@ -128,7 +128,7 @@
|
|
128
128
|
|
129
129
|
|
130
130
|
|
131
|
-
|id|name|x|y|create_time|cnt_0_0|cnt_0_1|cnt_1_0|cnt_1_1|
|
131
|
+
|id|name|x|y|create_time|cnt_0_0|cnt_0_1|cnt_1_0|cnt_1_1|
|
132
132
|
|
133
133
|
|--:|--:|--:|--:|--:|--:|--:|--:|--:|
|
134
134
|
|
2
しゅうせい
test
CHANGED
@@ -75,3 +75,77 @@
|
|
75
75
|
|0|1|2019-11-20 03:50:00|1|
|
76
76
|
|
77
77
|
|0|1|2019-11-20 05:50:00|1|
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
# 修正版
|
82
|
+
|
83
|
+
```SQL
|
84
|
+
|
85
|
+
create table tbl(id int primary key auto_increment,name varchar(10),x int,y int,create_time datetime,
|
86
|
+
|
87
|
+
index(name,x,y,create_time));
|
88
|
+
|
89
|
+
insert into tbl(name,x,y,create_time) values
|
90
|
+
|
91
|
+
('A',0,0,'2019-11-20 01:00'),
|
92
|
+
|
93
|
+
('A',0,1,'2019-11-20 01:10'),
|
94
|
+
|
95
|
+
('B',0,1,'2019-11-20 01:20'),
|
96
|
+
|
97
|
+
('A',0,1,'2019-11-20 01:30'),
|
98
|
+
|
99
|
+
('A',1,1,'2019-11-20 01:40'),
|
100
|
+
|
101
|
+
('B',1,1,'2019-11-20 01:50'),
|
102
|
+
|
103
|
+
('A',0,1,'2019-11-20 02:00'),
|
104
|
+
|
105
|
+
('A',0,1,'2019-11-20 03:50'),
|
106
|
+
|
107
|
+
('B',0,1,'2019-11-20 05:50');
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
select a.*,
|
112
|
+
|
113
|
+
coalesce((select sum(x=0 and y=0) from tbl where a.create_time>create_time and a.name=name),0) as cnt_0_0,
|
114
|
+
|
115
|
+
coalesce((select sum(x=0 and y=1) from tbl where a.create_time>create_time and a.name=name),0) as cnt_0_1,
|
116
|
+
|
117
|
+
coalesce((select sum(x=1 and y=0) from tbl where a.create_time>create_time and a.name=name),0) as cnt_1_0,
|
118
|
+
|
119
|
+
coalesce((select sum(x=1 and y=1) from tbl where a.create_time>create_time and a.name=name),0) as cnt_1_1
|
120
|
+
|
121
|
+
from tbl as a
|
122
|
+
|
123
|
+
order by id;
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
- 結果
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|id|name|x|y|create_time|cnt_0_0|cnt_0_1|cnt_1_0|cnt_1_1||
|
132
|
+
|
133
|
+
|--:|--:|--:|--:|--:|--:|--:|--:|--:|
|
134
|
+
|
135
|
+
|1|A|0|0|2019-11-20 01:00:00|0|0|0|0|
|
136
|
+
|
137
|
+
|2|A|0|1|2019-11-20 01:10:00|1|0|0|0|
|
138
|
+
|
139
|
+
|3|B|0|1|2019-11-20 01:20:00|0|0|0|0|
|
140
|
+
|
141
|
+
|4|A|0|1|2019-11-20 01:30:00|1|1|0|0|
|
142
|
+
|
143
|
+
|5|A|1|1|2019-11-20 01:40:00|1|2|0|0|
|
144
|
+
|
145
|
+
|6|B|1|1|2019-11-20 01:50:00|0|1|0|0|
|
146
|
+
|
147
|
+
|7|A|0|1|2019-11-20 02:00:00|1|2|0|1|
|
148
|
+
|
149
|
+
|8|A|0|1|2019-11-20 03:50:00|1|3|0|1|
|
150
|
+
|
151
|
+
|9|B|0|1|2019-11-20 05:50:00|0|1|0|1|
|
1
かくにn
test
CHANGED
@@ -43,3 +43,35 @@
|
|
43
43
|
from tbl as a;
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
# 確認
|
50
|
+
|
51
|
+
「●期待する結果」がなぜそういう結果になるかわかりません
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
自分のcreate_timeより前のx=1かつy=2の件数だったらこうなりませんか?
|
56
|
+
|
57
|
+
|x|y|create_time|count|
|
58
|
+
|
59
|
+
|--:|--:|--:|--:|
|
60
|
+
|
61
|
+
|0|0|2019-11-20 01:00:00|NULL|
|
62
|
+
|
63
|
+
|0|1|2019-11-20 01:10:00|0|
|
64
|
+
|
65
|
+
|0|2|2019-11-20 01:20:00|0|
|
66
|
+
|
67
|
+
|0|1|2019-11-20 01:30:00|0|
|
68
|
+
|
69
|
+
|1|2|2019-11-20 01:40:00|0|
|
70
|
+
|
71
|
+
|1|1|2019-11-20 01:50:00|1|
|
72
|
+
|
73
|
+
|2|0|2019-11-20 02:00:00|1|
|
74
|
+
|
75
|
+
|0|1|2019-11-20 03:50:00|1|
|
76
|
+
|
77
|
+
|0|1|2019-11-20 05:50:00|1|
|