Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL获取每个国家的人均工作工资_Sql_Sql Server_Database_Sql Server 2008 - Fatal编程技术网

SQL获取每个国家的人均工作工资

SQL获取每个国家的人均工作工资,sql,sql-server,database,sql-server-2008,Sql,Sql Server,Database,Sql Server 2008,Microsoft SQL Server 2008 一个工人生活在某个国家,可能有一份以上的工作,有一份以上的工资 我想得到每个国家个人总工资的平均值 表: 1个国家/地区中心id、名称 2个人序号、姓名、国家编号 3个职位SN、职位、薪水 [国家] 1、美国 2、德国 [人] 010101,约翰,1 020202,李,1 030303,哈利,2 [工作] 010101,老师,3200 010101,建筑商,1500 020202,演员,45000 020202,歌手,200000 030303

Microsoft SQL Server 2008

一个工人生活在某个国家,可能有一份以上的工作,有一份以上的工资

我想得到每个国家个人总工资的平均值

表:

1个国家/地区中心id、名称

2个人序号、姓名、国家编号

3个职位SN、职位、薪水

[国家]

1、美国 2、德国 [人]

010101,约翰,1 020202,李,1 030303,哈利,2 [工作]

010101,老师,3200 010101,建筑商,1500 020202,演员,45000 020202,歌手,200000 030303,制作人,120000 需要的查询结果: 每个国家的平均值=每个工人的工资总额/工人人数

国家平均工资

美国-124850 德国-120000 试试这个:

SELECT c.name, sum(j.salary)/count(distinct p.ssn) as Avg_Sal 
FROM Countries c
INNER JOIN people p ON c.country_id = p.country_id
INNER JOIN Jobs j on p.ssn = j.ssn
group by c.name

这应该适合您:

select Salary / count(c.name) AvgSalary, c.Name
from people p
inner join 
(
  select sum(salary) Salary, p.country_id
  from jobs j
  left join people p
    on j.ssn = p.ssn
  group by p.country_id
) sal
  on p.country_id = sal.country_id
left join countries c
  on p.country_id = c.country_id
group by salary, c.Name;
请看

这正好可以

with
country as
(select c.country_id,c.name name,p.ssn ssn from countries c,people p where c.country_id=p.country_id),
sal1 as
(select sum(salary) salary,j.ssn ssn from people p,jobs j where p.ssn=j.ssn group by j.ssn)
select country.name,avg(sal1.salary) from sal1,country where sal1.ssn=country.ssn group by country.name;

使用with子句将更具可读性和易懂性。

您需要使用SUM COUNT和GROUP BY-sql的哪个版本?SQL Server、MySQL、Oracle?