带有Sum和Group By的SQL查询

时间:2021-07-22 07:43:46

I have a table similar to the example below.

我有一个类似于下面示例的表。

Field1 Field2
Chris   100
Chris   200
John    50
Maria   250
John    80

I want to achieve the result shown below.

我想实现如下所示的结果。

Chris 300
John  130
Maria 250

I've already achieved this by using 2 separate sql statements. I'm wondering if it can be achieved in a single SQL statement to speed up querying. Thank you very much.

我已经通过使用两个独立的sql语句实现了这一点。我想知道是否可以在一个SQL语句中实现它来加速查询。非常感谢。

2 个解决方案

#1


2  

Group by the field you want to sum over and then use sum on the column you want to sum

根据你想要求和的字段,然后在你想要求和的列上使用sum。

SELECT Field1, SUM(Field2) as Field2
FROM Mytable
GROUP BY Field1

#2


1  

Should be able with:

应该能够:

SELECT field1, SUM(field2) FROM table GROUP BY field1

shouldn't it?

不应该吗?

#1


2  

Group by the field you want to sum over and then use sum on the column you want to sum

根据你想要求和的字段,然后在你想要求和的列上使用sum。

SELECT Field1, SUM(Field2) as Field2
FROM Mytable
GROUP BY Field1

#2


1  

Should be able with:

应该能够:

SELECT field1, SUM(field2) FROM table GROUP BY field1

shouldn't it?

不应该吗?