4

我有一张这样的桌子:

A      B        C         Value
1990    1   1   0.791684449
1990    1   2   0.058315583
1990    2   3   2.940000057
1990    3   4   8.975000381
1990    4   5   0
1990    4   6   0.004666695
1990    4   7   0.401333332
1990    5   8   2.609000206
1990    6   9   6.400992393
1990    6   10  9.489910126
1990    6   11  1.31809783
1990    7   12  214.4270172
1990    8   13  0.097975887
1990    8   14  0.680250943
1990    8   15  9.270773888
1990    9   16  1.204782724
1990    9   17  4.113919258
1990    9   18  3.486953974
1990    9   19  1.675345063
1990    10  20  0.434966862
1990    10  21  2.561163187
1990    10  22  2.415041685
1990    10  23  2.660344124
1990    10  24  7.127485275

现在我只想添加那些 B 相同并根据 C 显示在表中的值,例如:

1990        1   0.850000031
1990        2   2.940000057
1990        3   8.975000381
1990        4   0.406000027
1990        5   2.609000206
1990        6   17.20900035

我怎样才能在 MySQL 中做到这一点?

4

4 回答 4

3

使用GROUP BYSUM

试试这个 SQL:

SELECT A, B, SUM(Value) AS TotalValue
FROM yourtable
GROUP BY A, B

在线查看它:sqlfiddle

有关更多详细信息,请参阅文档:

于 2012-10-23T08:09:12.500 回答
2

使用AGGREGATE FUNCTION SUM()计算总值并按 和A分组B

SELECT A, B, SUM(c) totalValue
FROM tableName
GROUP BY A, B

SQLFiddle 演示

于 2012-10-23T08:09:08.053 回答
0

按 B,A 从表组中选择 A,B, sum(value)

于 2012-10-23T08:10:18.580 回答
0

试试这个:

SELECT A, min(c) c,SUM(value) totalValue
FROM tableName
GROUP BY A, B
于 2012-10-23T08:18:09.393 回答