MySQL:获取日常增量数据

时间:2015-10-23 15:03:09

标签: mysql

我想基于date从表中获取数据,但是以增量方式获取。

假设我有这样的数据按日期分组

| DATE       | Count |
| 2015-06-23 | 10    |
| 2015-06-24 | 8     |
| 2015-06-25 | 6     |
| 2015-06-26 | 3     |
| 2015-06-27 | 2     |
| 2015-06-29 | 2     |
| 2015-06-30 | 3     |
| 2015-07-01 | 1     |
| 2015-07-02 | 3     |
| 2015-07-03 | 4     |

所以结果应该是这样的

| DATE       | Count| Sum|
| 2015-06-23 | 10   | 10 |
| 2015-06-24 | 8    | 18 |
| 2015-06-25 | 6    | 24 |
| 2015-06-26 | 3    | 27 |
| 2015-06-27 | 2    | 29 |
| 2015-06-29 | 2    | 31 |
| 2015-06-30 | 3    | 34 |
| 2015-07-01 | 1    | 35 |
| 2015-07-02 | 3    | 38 |
| 2015-07-03 | 4    | 42 |

1 个答案:

答案 0 :(得分:0)

您将在该日期加入之前的所有其他日期,然后将该计数加起来 如果你给我你的表结构,我可以让它运行。  id,name,date_joined

SELECT counts.theCount, sum(counts.theCount), table.date_joined
FROM yourTable
LEFT JOIN 
(SELECT count(*) as theCount, table.date_joined
 FROM yourTable
 GROUP BY table.date_joined
) as counts
ON
yourTable.date_joined> counts.date_joined
GROUP BY yourTable.date_joined