0
我有以下MySQL表:
Timestamp | MachineID | Arrival_Timestamp | CobID | Data |
--------------------+-----------+---------------------+-------+------|
2017-07-22 20:03:07 | 1217 | 2017-08-08 04:46:02 | C237 | 10 |
2017-07-22 20:03:08 | 1217 | 2017-08-08 04:47:03 | C237 | 11 |
2017-07-22 20:03:09 | 1217 | 2017-08-08 04:48:04 | C237 | 12 |
2017-07-22 20:07:17 | 1218 | 2017-08-08 04:49:05 | C234 | 20 |
2017-07-22 20:05:19 | 1224 | 2017-08-08 04:50:06 | C239 | 30 |
2017-07-22 20:06:17 | 1216 | 2017-08-08 04:50:07 | C237 | 40 |
2017-07-22 20:08:07 | 1224 | 2017-08-08 04:50:08 | C234 | 50 |
2017-07-22 20:09:14 | 1216 | 2017-08-08 04:55:09 | C239 | 60 |
2017-07-22 20:09:14 | 1216 | 2017-08-08 04:55:10 | C239 | 61 |
2017-07-22 20:09:17 | 1216 | 2017-08-08 04:55:12 | C239 | 63 |
2017-07-22 20:09:18 | 1216 | 2017-08-08 04:56:02 | C239 | 63 |
2017-07-22 20:09:19 | 1216 | 2017-08-08 04:56:03 | C239 | 63 |
2017-07-22 20:09:34 | 1217 | 2017-08-08 04:56:21 | C234 | 70 |
2017-07-22 20:09:56 | 1224 | 2017-08-08 04:57:23 | C237 | 80 |
2017-07-22 20:09:58 | 1299 | 2017-08-08 04:57:43 | C239 | 90 |
2017-07-22 20:09:59 | 1218 | 2017-08-08 04:57:55 | C239 | 100 |
2017-07-22 20:10:04 | 1218 | 2017-08-08 04:58:03 | C234 | 110 |
2017-07-22 20:10:17 | 1217 | 2017-08-08 04:58:04 | C239 | 120 |
從這個表,我想指望所有條目/所有獨特的MachineID
s的所有獨特的CobID
行。
目標:
我想寫一個查詢,其中輸出將是:
| 1216 | 1217 | 1218 | 1224 | 1299 |
-----+------+------+------+------+------|
C234 | 0 | 1 | 1 | 2 | 0 |
C237 | 1 | 3 | 0 | 1 | 0 |
C239 | 5 | 1 | 1 | 1 | 1 |
所以這個查詢,我可以看看MachineID
1216
說有0
條目C234
而C239
有5
條目。
我迄今所做的:
我曾嘗試下面的查詢,這讓我我想要的東西,而不是在正確的格式我想:
data_query = "SELECT `MachineID`, `CobID`, COUNT(*) FROM `my_table` WHERE `Arrival_Timestamp` between \'{}\' AND \'{}\' GROUP BY `CobID`, `MachineID` ORDER BY `CobID` ASC".format(
start_date, end_date)
問題:
如何更改查詢以使其達到上述格式?