MySQL查询组由连续的外键值组成

时间:2014-02-20 00:08:40

标签: mysql sql

我有这样的数据:

+----+-------------------------+----------+----------+
| 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求和。但是,我希望将QuantityVendorID分组,只要它们相邻(按DateReceived排序),就像下面的示例输出一样。

+----------+----------+
| VendorID | Quantity |
+----------+----------+
|        1 |        5 |
|        2 |        3 |
|        1 |        1 |
|        3 |       10 |
|        1 |        9 |
|        2 |        2 |
+----------+----------+

我认为答案与变量有关,但我无法思考如何去做。

什么是用于生成所需输出的MySQL查询?

注意:我问了exact same thing here但是对于MS Sql,我现在需要这个用于MySQL。

1 个答案:

答案 0 :(得分:0)

select 
VendorID,
SUM(Quantity)
from (
select
t.*,
@grn := if(@prev != VendorID, @grn + 1, @grn) as grn,
@prev := VendorID
from
t
, (select @grn := 0, @prev := null) var_init
order by DateReceived
) sq
GROUP BY grn

但是你的预期输出是错误的。你可以通过执行这个来看到这个:

select
t.*,
@grn := if(@prev != VendorID, @grn + 1, @grn) as grn,
@prev := VendorID
from
t
, (select @grn := 0, @prev := null) var_init
order by DateReceived

结果是:

| ID |                    DATERECEIVED | QUANTITY | VENDORID | GRN | @PREV := VENDORID |
|----|---------------------------------|----------|----------|-----|-------------------|
|  3 |     July, 26 2010 07:52:29+0000 |        2 |        1 |   0 |                 1 |
|  2 |   August, 01 2010 13:31:26+0000 |        1 |        1 |   0 |                 1 |
|  1 |   August, 09 2010 06:53:44+0000 |        2 |        1 |   0 |                 1 |
|  7 |    March, 18 2011 09:04:58+0000 |        1 |        1 |   0 |                 1 |
|  6 |    March, 22 2011 11:27:01+0000 |        1 |        2 |   1 |                 2 |
|  4 |    March, 22 2011 13:31:11+0000 |        1 |        2 |   1 |                 2 |
|  5 |    March, 22 2011 13:31:11+0000 |        1 |        2 |   1 |                 2 |
|  8 | December, 17 2011 08:21:29+0000 |        1 |        3 |   2 |                 3 |
| 12 |     July, 05 2012 20:45:29+0000 |        1 |        1 |   3 |                 1 |
| 11 |     July, 12 2012 20:44:36+0000 |        3 |        1 |   3 |                 1 |
| 10 |   August, 02 2012 20:18:10+0000 |        5 |        1 |   3 |                 1 |
|  9 |   August, 10 2012 10:55:20+0000 |        9 |        3 |   4 |                 3 |
| 13 |    March, 22 2013 13:31:11+0000 |        1 |        2 |   5 |                 2 |
| 14 |    March, 22 2013 13:31:11+0000 |        1 |        2 |   5 |                 2 |

整个查询的结果:

| VENDORID | SUM(QUANTITY) |
|----------|---------------|
|        1 |             6 |
|        2 |             3 |
|        3 |             1 |
|        1 |             9 |
|        3 |             9 |
|        2 |             2 |