总和行组

时间:2011-11-11 15:26:27

标签: mysql group-by

假设我有一张这样的表

+------------+---------+
| fecha      | cuota   |
+------------+---------+
| 2000-12-17 | 2345.06 |
| 2007-02-26 | 1666.45 |
| 2007-02-26 | 4011.51 |
| 2007-02-26 | 4011.51 |
| 2007-02-26 | 4011.51 |
| 2007-02-26 | 4011.51 |
| 2007-02-26 | 2287.52 |
| 2007-12-28 | 1723.99 |
| 2007-12-28 | 4011.51 |
| 2007-12-28 | 4011.51 |
| 2007-12-28 | 252.99  |
+------------+---------+

如何将具有相同日期的行相加以获得类似

的行
+------------+---------+
| fecha      | cuota   |
+------------+---------+
| 2000-12-17 | 2345.06 |
| 2007-02-26 | 2000.00 |
| 2007-12-28 | 10000.00|
+------------+---------+

2 个答案:

答案 0 :(得分:3)

您可以尝试:

SELECT fecha, SUM(cuota) as cuota
FROM myTable
GROUP BY fecha;

简单地说,它会将所有日期组合在一起,然后将每个组中的所有cuota值相加。

答案 1 :(得分:1)

这有用吗?

select fecha, sum(cuota) from yourTable group by fecha