使用SQL查找捐赠给组织的总金额

时间:2021-05-17 01:05:13

I have a SQL table with 2 columns,

我有一个包含2列的SQL表,

organization_id,
amount_donated

I want to return a new table with 2 columns,

我想返回一个包含2列的新表,

organization_id 
total_amount_donated

How would I go about finding the total amount donated to each organization? I am thinking I have to use GROUP BY but I haven't been able to find a solution.

我如何查找捐赠给每个组织的总金额?我想我必须使用GROUP BY,但我找不到解决方案。

3 个解决方案

#1


1  

You're right, you do want to use GROUP BY. This is because you need to use the SUM aggregate function, and by using GROUP BY organization_id you will sum the amounts corresponding to each organization.

你没错,你确实想要使用GROUP BY。这是因为您需要使用SUM聚合函数,并且通过使用GROUP BY organization_id,您将对与每个组织相对应的金额求和。

SELECT organization_id, SUM(amount_donated) AS total_amount_donated
FROM your_table
GROUP BY organization_id

This is pretty simple SQL, you should probably get your hands on a tutorial or a book to step you through the basics. :)

这是非常简单的SQL,您应该可以通过教程或书籍来指导您完成基础知识。 :)

#2


0  

You should't to create table, because it will create duplicate data. Instead create a VIEW.

你不应该创建表,因为它会创建重复的数据。而是创建一个VIEW。

CREATE VIEW vw_total_donation
AS
SELECT organization_id, SUM(amount_donated) tot_donation
FROM   table1
GROUP BY organization_id

#3


0  

select organisation_id,sum(amount_donated) as total_amt_donated from your_table
group by organisation_id

You can query like this

你可以像这样查询

#1


1  

You're right, you do want to use GROUP BY. This is because you need to use the SUM aggregate function, and by using GROUP BY organization_id you will sum the amounts corresponding to each organization.

你没错,你确实想要使用GROUP BY。这是因为您需要使用SUM聚合函数,并且通过使用GROUP BY organization_id,您将对与每个组织相对应的金额求和。

SELECT organization_id, SUM(amount_donated) AS total_amount_donated
FROM your_table
GROUP BY organization_id

This is pretty simple SQL, you should probably get your hands on a tutorial or a book to step you through the basics. :)

这是非常简单的SQL,您应该可以通过教程或书籍来指导您完成基础知识。 :)

#2


0  

You should't to create table, because it will create duplicate data. Instead create a VIEW.

你不应该创建表,因为它会创建重复的数据。而是创建一个VIEW。

CREATE VIEW vw_total_donation
AS
SELECT organization_id, SUM(amount_donated) tot_donation
FROM   table1
GROUP BY organization_id

#3


0  

select organisation_id,sum(amount_donated) as total_amt_donated from your_table
group by organisation_id

You can query like this

你可以像这样查询