基于另一列中的日期对列中的值求和并对其进行分组

时间:2013-06-19 06:34:08

标签: sql

我在数据库中有以下表格。我希望能够获得值列的总和,但仅基于日期。

+-------+---------+------------+
| ID    | value   | date       |
+-------+---------+------------+
|   1   |   3     | 2011-12-22 |
|   1   |   2     | 2011-12-22 |
|   1   |   4     | 2011-12-22 |
|   1   |   2     | 2012-01-01 |
+-------+---------+------------+

应该是

+-------+---------+------------+
| ID    | value   | date       |
+-------+---------+------------+
|   1   |   9     | 2011-12-22 |
|   1   |   2     | 2012-01-01 |
+-------+---------+------------+

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

Select id,sum(`value`) `value` , DATE(`date`) from table_name group by DATE(`date`);

答案 1 :(得分:0)

SELECT ID, SUM(value), date FROM table_name GROUP BY date;