Microsoft SQL Server 2008
A worker lives in some Country, and may have more than one job, with more than one salary.
I want to get the average for each country’s individual total salary.
Tables:
1) countries(country_id, name)
2) people(ssn, name,country_id)
3) jobs(ssn, job_title, salary)
[countries]
- 1, usa
- 2, germany
[people]
- 010101, John, 1
- 020202, Lee, 1
- 030303, Harry, 2
[jobs]
- 010101, teacher, 3200
- 010101, builder, 1500
- 020202, actor, 45000
- 020202, singer, 200000
- 030303, producer, 120000
Needed Query Result:
each country’s average (for each country) = sum of each worker’s total salaries) / number of workers
Country – Average Salary
- usa – 124850
- germany – 120000
This should work for you:
See SQL Fiddle with Demo