具有不同 ID 的列的总和? [英] Sum of a column with distinct ID?
问题描述
我有一个包含多个 ID 的表,需要一个查询来返回每个 ID 的列的总和.Calls_table 看起来像这样
i have a table that has multiple IDs and need a query to return the sum of a column for each ID. Calls_table looks like this
EmployeeID TypeOfCall InvoiceAmount
John NC 50
john NC 100
Joe NC 76
Joe NC 50
我有它,所以我现在必须像员工一样由员工来做
i have it so i have to do it employee by emplyee now like
SELECT sum(InvoiceAmount/2) as "Total Calls"
from Calls
where TypeOfCall='NC' and EmployeeID='Derek';
但我希望它能够返回这样的列表中的所有 ID
but i would like it to be able to return all IDs in a list like this
Total Calls
Joe 100
John 68
我确定我需要使用 Distinct 参数,但就是不知道在哪里
I am sure i need to use the Distinct parameter but just cant figure out where
推荐答案
需要使用group by关键字
You need to use the group by keyword
Select EmployeeID, SUM(InvoiceAmount)
From Calls
Group by EmployeeID
您甚至可以更进一步,按呼叫类型和 EmployeeID 进行分组,如下所示:
You can even take it a bit further and group by type of call and EmployeeID like This:
Select EmployeeID, TypeOfCall, SUM(InvoiceAmount)
From Calls
Group by EmployeeID, TypeOfCall
执行此操作时,您选择的字段需要是聚合函数(sum、count、avg 等)或在 Group by 中.
Your selected fields need to be either aggregate functions (sum, count, avg, etc) or in the Group by when doing this.
这篇关于具有不同 ID 的列的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!