oracle 如何根据SQL中两列的值获取列的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19107468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:57:50  来源:igfitidea点击:

How to get sum of a column based on the values of two columns in SQL

sqldatabaseoracle

提问by Roshanck

I have a table as bellow with 3 columns. Sample data is given

我有一个如下表,有 3 列。给出了样本数据

column_1    column_2    column_3
   A           a           10
   A           b           20
   B           a           10
   A           a           10
   B           a           30
   A           b           40
   A           c           10
   C           a           20   

I want to get the sum of column_3 based on the values of column_1 and column_2. Which means I want to get the sum of values in column_3 which are having 'A' for column_1 'a' for column_2 and so on..

我想根据 column_1 和 column_2 的值获得 column_3 的总和。这意味着我想获得 column_3 中的值的总和,其中 column_1 为 'A',column_2 为 'a' 等等。

Sample output is given bellow
     column_1    column_2    SUM(column_3)
       A           a              20
       A           b              60
       A           c              10
       B           a              40
       C           a              20        

Can someone please tell me how to do this

有人可以告诉我怎么做吗

回答by Zack Newsham

Try this:

尝试这个:

SELECT SUM(column_3), column_1, column_2 FROM table GROUP BY column_1, column_2

The GROUP BYcommand states that you want to group the aggregate function (SUM) using distinct values of the columns which names follow the GROUP BY. It is not "required" that they also appear in the SELECTportion of the query, but it is good practice (and the only way of knowing which row represents which grouping.

GROUP BY命令指出您要SUM使用名称跟在GROUP BY. 它们并不“必须”出现在SELECT查询的部分中,但这是一种很好的做法(也是了解哪一行代表哪个分组的唯一方法。

Obviously replace tablewith the actual table name.

显然替换table为实际的表名。

回答by SeeSharp

can you use group by with col1 and col2 and sum the col3 like below

您可以将 group by 与 col1 和 col2 一起使用并将 col3 相加,如下所示

SELECT   SUM (col3)
    FROM test_table
GROUP BY col1, col2

回答by blaklaybul

select column_1, column_2, SUM(column_3) from table group by column_1, column_2

select column_1, column_2, SUM(column_3) from table group by column_1, column_2

group by allows you to apply an aggregate function to a column based on groupings of other columns.

group by 允许您根据其他列的分组将聚合函数应用于列。

Thinking backwards, group by column_1, column_2can be thought of as: "give me all unique combinations of column_1 and column_2. Selecting SUM(column_3)will then take the sum of the entries in column_3 for each group.

group by column_1, column_2回想一下,可以认为:“给我 column_1 和 column_2 的所有唯一组合。SUM(column_3)然后选择将取每个组的 column_3 中条目的总和。

回答by Vijay

try out this...

试试这个...

SELECT SUM(column_3), column_1, column_2 FROM table   
GROUP BY column_1, column_2