derek
derek

Reputation: 123

Sum of a column with distinct ID?

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';

but i would like it to be able to return all IDs in a list like this

Total Calls
Joe              100
John             68

I am sure i need to use the Distinct parameter but just cant figure out where

Upvotes: 2

Views: 11721

Answers (2)

Melanie
Melanie

Reputation: 3111

SELECT EmployeeID, SUM(InvoiceAmount/2)
FROM Calls
WHERE TypeOfCall='NC'
GROUP BY EmployeeID

Upvotes: 1

Chad
Chad

Reputation: 1562

You need to use the group by keyword

Select EmployeeID, SUM(InvoiceAmount) 
From Calls
Group by 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

Your selected fields need to be either aggregate functions (sum, count, avg, etc) or in the Group by when doing this.

Upvotes: 9

Related Questions