1
我有數據這樣的分組:MySQL查詢通過contigious外鍵值
+----+-------------------------+----------+----------+
| ID | DateReceived | Quantity | VendorID |
+----+-------------------------+----------+----------+
| 1 | 2010-08-09 06:53:44.783 | 2 | 1 |
| 2 | 2010-08-01 13:31:26.893 | 1 | 1 |
| 3 | 2010-07-26 07:52:29.403 | 2 | 1 |
| 4 | 2011-03-22 13:31:11.000 | 1 | 2 |
| 5 | 2011-03-22 13:31:11.000 | 1 | 2 |
| 6 | 2011-03-22 11:27:01.000 | 1 | 2 |
| 7 | 2011-03-18 09:04:58.000 | 1 | 1 |
| 8 | 2011-12-17 08:21:29.000 | 1 | 3 |
| 9 | 2012-08-10 10:55:20.000 | 9 | 3 |
| 10 | 2012-08-02 20:18:10.000 | 5 | 1 |
| 11 | 2012-07-12 20:44:36.000 | 3 | 1 |
| 12 | 2012-07-05 20:45:29.000 | 1 | 1 |
| 13 | 2013-03-22 13:31:11.000 | 1 | 2 |
| 14 | 2013-03-22 13:31:11.000 | 1 | 2 |
+----+-------------------------+----------+----------+
我想通過DateReceived
對數據進行排序,總結了Quantity
。但是,只要它們相鄰(當按DateReceived
排序時),我想要將Quantity
總和爲VendorID
,如下面的示例輸出。
+----------+----------+
| VendorID | Quantity |
+----------+----------+
| 1 | 5 |
| 2 | 3 |
| 1 | 1 |
| 3 | 10 |
| 1 | 9 |
| 2 | 2 |
+----------+----------+
我認爲答案與變量有關,但我不知道如何去做。
什麼是MySQL查詢來生成所需的輸出?
注意:我問exact same thing here但是對於MS Sql,我現在需要這個用於MySQL。
你是怎麼得到'| | 1 | 9 |'行?請解釋。 –
@ PM77-1通過將'VendorID'的'Quantity'字段相加1.(行ID 10-12) – Icode4food