如何对一组类似的结果进行分组并为SQL中的一个最终结果添加值?

时间:2020-12-30 07:56:44

I know there has to be a simple answer to this question, but I am a total SQL noob. In my result set I have multiple results in one column that have one specific value that is repeated several times. In another column there is another value that needs to be added. Please see the attached photo for clarification. 如何对一组类似的结果进行分组并为SQL中的一个最终结果添加值?

我知道这个问题必须有一个简单的答案,但我是一个完整的SQL菜鸟。在我的结果集中,我在一列中有多个结果,这些结果具有一个重复多次的特定值。在另一列中,还有另一个值需要添加。请参阅附图以获得澄清。

I want to be able to find all of the values in Column B that correspond to 'A' and add them up for one result like shown. I want to be sure to delete the duplicates in Column A. Any help is greatly appreciated.

我希望能够找到B列中与'A'对应的所有值,并将它们添加到一个结果中,如图所示。我想确保删除A列中的重复项。非常感谢任何帮助。

1 个解决方案

#1


4  

You're trying to SUM the values in column B and you're trying to group them by (GROUP BY) the value in column A when you do that. This is accomplished like so:

您正在尝试对B列中的值进行求和,并且当您这样做时,您尝试按(GROUP BY)对A组中的值进行分组。这是这样完成的:

SELECT
    col_a,
    SUM(col_b)
FROM
    My_Table
GROUP BY
    col_a

#1


4  

You're trying to SUM the values in column B and you're trying to group them by (GROUP BY) the value in column A when you do that. This is accomplished like so:

您正在尝试对B列中的值进行求和,并且当您这样做时,您尝试按(GROUP BY)对A组中的值进行分组。这是这样完成的:

SELECT
    col_a,
    SUM(col_b)
FROM
    My_Table
GROUP BY
    col_a