Sum of a column with distinct ID? - sql

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

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.

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

Related

How to show the average and use it like condition in the same query?

I am working with a data base and I need to show the people who are older than the age average in a city comparing their age against that average. My code shows the people who is older than the average....but I can't show the average of all the people (it's allways the same number) in each line.
SELECT name, age FROM people
WHERE age > (SELECT AVG(age) FROM people);
I need to show something like this:
name age average
Mick 18 17.5
Sarah 25 17.5
Joe 38 17.5
Any help, please.
You can write the same subquery to calculate the average age within select list:
SELECT name, age, (SELECT AVG(age) FROM people) average FROM people
WHERE age > (SELECT AVG(age) FROM people);
Or if your database allows window function you can do this:
select name,age,average from
(
SELECT name, age, AVG(age) over() average FROM people
)t where age>average

Selected columns that are not in group by

I am a quetions about SQL. For example i have a table like this
Name Surname Price Adress
john smith 100 adress123
alex martin 200 adress2
john smith 300 adress123
And i want to group this records which name is same.And this records prices must be sum()
Generally i write query like this
SELECT SUM(PRICE),NAME AS TOTAL_PRICE FROM TABLE1 A GROUP BY A.NAME
But when i want to select other columns i should be group by like this
... group by A.NAME,A.SURNAME,A.ADRESS
I want select this columns without group by. I want to ask what is the best way selecting other columsn without using group by condition?
I am waiting this result
200 ALEX MARTIN adress2
210 JOHN SMITH adress123
but i don't want to group by surname and adress column
You can get an arbitrary value from the rest of the columns by using MIN() or MAX():
select sum(price) as total_price, name, max(surname) as surname,
max(address) as address
from table1 a
group by a.name;

How to get Total of counts?

I'm trying to get Total number of count per column. Here is example of what I need:
Grade Count Name
9 1 Jon
10 3 Ash
I would like to get Total under my Count column what will give the sum of 1 and 3.
Here is my query:
select grade, count(*) as count, name
from students
group by grade, name
order by grade, name;
Thanks in advance.
Many databases support the ROLLUP clause. If yours does, you can just do something like this:
select grade, count(*) as count, name
from students
group by grade, name with rollup
order by grade, name;
Technically, you would want the columns where the grade and rollup are both NULL, but this will give you partial totals as well.

Filter Rows by highest version

I have a selection of Data. example shown in SQL Fiddle
I want to return the highest myValue for each name.
NAME MYVALUE
A1 22
A2 22
A3 21
A4 36
A6 12
A9 5
There are 3 rows named A6, I only want the row with the highest value returned.
SELECT NAME,
max(MYVALUE)
FROM TABLE t
GROUP BY NAME
SQL Fiddle
I find analytic functions a nice way to achieve these kinds of results. In your case that would mean something like:
SELECT
name
, MAX(myvalue) OVER (PARTITION BY name) AS max_name_value
FROM mytable;
Analytic queries IMO offer more flexibility than using a GROUP BY alone. The article to which I've linked offers the following example:
We want to see a list of all departments (that have employees) with the average salary in that department. The following two queries give
the same result, one, traditional, with and one without using the
group by. The second, analytical query, can more easily be extended to
return other information as well, such as an aggregation at a
different grouping level.
select d.dname
, avg(e.sal)
from dept d
natural join
emp e
group
by d.dname
Using an analytical function:
select distinct
d.dname
, avg(e.sal) over (partition by d.dname) Average_Salary_in_Dept
from dept d
natural join
emp e
Read the analytical part of this query as follows: 'for each record
returned, select the average of salary values for all rows in the
partition (subset, group) with the same department-name as the current
record’s department-name'.
You can also partition by more than one column to show, for example, the maximum ID for each unique combination of name and department, simply by adding more column names to the PARTITION BY statement as follows: (PARTITION BY name, department).

Need a hand with a simple query

I need a help with a query. I think is not so difficult.
I need to do a select with distinct and at the same time, do a count(*) of how many rows are returned by this distinct.
One example:
Table names>
Id Name
1 john
2 john
3 mary
I need a query thats return:
Name Total
john 2
mary 1
select name, count(*) from names group by name;
SELECT name, COUNT(*) FROM names GROUP BY name
SELECT name, count(*) as occurrences FROM names GROUP BY name