sql - 嵌套 SQL 聚合函数查询以获取每位经理的最低工资员工 : My Query Is Not Working

标签 sql sql-server-2008 subquery

以下是我要回答的提示:

显示经理编号以及该经理工资最低的员工的工资(仅) - 适当标记。不包括经理未知的员工的工资。排除此处的任何群体,最低工资低于 1000 美元。首先列出最低工资。

以下是我的 EMPLOYEES 表:

create table EMPLOYEES
    (EmpID    char(4)         unique Not null,
     Ename    varchar(10),
     Job      varchar(9),
     MGR      char(4),
     Hiredate date,
     Salary   decimal(7,2),
     Comm     decimal(7,2),
     DeptNo   char(2)         not null,
         Primary key(EmpID),
         Foreign key(DeptNo) REFERENCES DEPARTMENTS(DeptNo));


insert into EMPLOYEES values (7839,'King','President',null,'17-Nov-11',5000,null,10);
insert into EMPLOYEES values (7698,'Blake','Manager',7839,'01-May-11',2850,null,30);
insert into EMPLOYEES values (7782,'Clark','Manager',7839,'02-Jun-11',2450,null,10);
insert into EMPLOYEES values (7566,'Jones','Manager',7839,'02-Apr-11',2975,null,20);
insert into EMPLOYEES values (7654,'Martin','Salesman',7698,'28-Feb-12',1250,1400,30);
insert into EMPLOYEES values (7499,'Allen','Salesman',7698,'20-Feb-11',1600,300,30);
insert into EMPLOYEES values (7844,'Turner','Salesman',7698,'08-Sep-11',1500,0,30);
insert into EMPLOYEES values (7900,'James','Clerk',7698,'22-Feb-12',950,null,30);
insert into EMPLOYEES values (7521,'Ward','Salesman',7698,'22-Feb-12',1250,500,30);
insert into EMPLOYEES values (7902,'Ford','Analyst',7566,'03-Dec-11',3000,null,20);
insert into EMPLOYEES values (7369,'Smith','Clerk',7902,'17-Dec-10',800,null,20);
insert into EMPLOYEES values (7788,'Scott','Analyst',7566,'09-Dec-12',3000,null,20);
insert into EMPLOYEES values (7876,'Adams','Clerk',7788,'12-Jan-10',1100,null,20);
insert into EMPLOYEES values (7934,'Miller','Clerk',7782,'23-Jan-12',1300,null,10);

以下是我的查询:

select empid, salary
from employees
where salary in
(select MIN(salary)
from employees
where empid in
(select empid
from EMPLOYEES
where JOB != 'manager'))
order by Salary asc; 

结果仅是除经理之外收入最低的人员。我需要每位经理工资最低的员工,包括总裁手下工资最低的经理。

最佳答案

select empid, salary
from employees
where salary in
(select MIN(salary)
from employees
where empid in
(select empid
from EMPLOYEES
where JOB != 'manager'
 group by EmpID
 )
 and
 JOB != 'manager' and MGR is not null
 group by mgr
 )
order by Salary asc;

关于sql - 嵌套 SQL 聚合函数查询以获取每位经理的最低工资员工 : My Query Is Not Working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16067775/

相关文章:

mysql - 为什么mysql会判定这个子查询是依赖的呢?

MySQL 无法识别虚拟列/字段

php - 不使用loop mysql php从大表中删除具有不同标识符的多条记录

mysql - 使用 LEFT JOIN、SUM() 和 HAVING() 选择 COUNT()

sql - 如何在SQL中将记录的首字母大写

SQl 存储小数

mysql - SQL - 如果相同的记录不在表 Y 中,则从表 X 中删除记录

python - Pandas,返回df,其中某一列的值为空

c# - 查找数据库中打开的连接数

mysql - 查询返回一条 "extra"记录。关于如何从查询结果中删除它的任何建议?