Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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_Postgresql - Fatal编程技术网

Sql 查找两个数据输出之间的差距

Sql 查找两个数据输出之间的差距,sql,postgresql,Sql,Postgresql,所以当我需要找出总员工的两个平均工资之间的差距时,我遇到了这个问题, 这意味着所有员工都在一个项目上工作,而所有员工都没有在一个项目上工作。 (如果您愿意,则属于a项目) 以下是相关表格: create table employee ( eid numeric(5,0), ename varchar(30), salary integer, did numeric(3,0), classification integer, primary key(

所以当我需要找出总员工的两个平均工资之间的差距时,我遇到了这个问题, 这意味着所有员工都在一个项目上工作,而所有员工都没有在一个项目上工作。 (如果您愿意,则属于a项目)

以下是相关表格:

create table employee (
    eid numeric(5,0),
    ename varchar(30),
    salary integer,
    did numeric(3,0),
    classification integer,
    primary key(eid),
    foreign key(did)references department
);

create table onproject(
    pid numeric(3,0),
    eid numeric(5,0),
    fdate date,
    primary key(pid,eid),
    foreign key (eid) references employee,
    foreign key (pid) references project
);
请记住,我不能改变表格的书写方式。 我的想法是为那些目前不属于任何项目的员工创建AVG,反之亦然 我成功地做到了这一点:

select count(employee.eid),avg(salary) 
from employee
where employee.eid not in select onproject.eid from onproject

但不幸的是,在这之后不久,您就可以通过左键将
employee
连接到
onproject
,然后使用条件聚合:

select
  avg(case when t.ison is not null then t.salary end) - 
  avg(case when t.ison is null then t.salary end) gap
from (  
  select e.eid, e.salary, max(o.eid) ison 
  from employee e left join onproject o
  on o.eid = e.eid
  group by e.eid, e.salary
) t  

你可以按以下步骤进行:

select
    avg(salary) filter(where has_project = 1) avg_salary_on_project
    avg(salary) filter(where has_project = 0) avg_salary_not_on_project
from (
    select 
        eid,
        salary,
        case when exists (select 1 from onproject o where o.eid = e.eid) 
            then 1
            else 0
        end has_project
    from employee e
) t

内部查询使用内联子查询来标识每个员工是否至少属于一个项目。这将生成一个名为has\u project的布尔列(
0/1
)。外部查询使用该列进行条件平均。

首先,找出您正在使用的RDBMS。然后看
LEFT JOIN
不知道这意味着什么…你是在使用mysql还是PostgreSQL PostgreSQL看,就是这样,他们可以同时处理2个甚至3个项目P.s(对不起,我忘了清楚地提到它非常重要)首先我要感谢你!如果你解释一下你在这里做了什么,可以吗:avg(如果t.ison不为null,那么t.salary end)-avg(如果t.ison为null,那么t.salary end)gap