我的任务是显示该经理的最低薪雇员的MGR和工资。
我需要排除MGR未知的任何人,并排除最低工资低于1000美元的任何群体。结果应按工资降序排列。
这是表格:
+-------+--------+-----------+------+------------+-----------+-----------+------+
| Empno | Ename | Job | Mgr | Hiredate | Sal | Comm | Dept |
+-------+--------+-----------+------+------------+-----------+-----------+------+
| 7839 | KING | PRESIDENT | | 11/17/1981 | $5,000.00 | $0.00 | 10 |
| 7782 | CLARK | MANAGER | 7839 | 6/9/1981 | $2,450.00 | $0.00 | 10 |
| 7934 | MILLER | CLERK | 7782 | 1/23/1982 | $1,300.00 | $0.00 | 10 |
| 7902 | FORD | ANALYST | 7566 | 12/3/1981 | $3,000.00 | $0.00 | 20 |
| 7788 | SCOTT | ANALYST | 7566 | 12/9/1982 | $3,000.00 | $0.00 | 20 |
| 7876 | ADAMS | CLERK | 7788 | 1/12/1983 | $1,100.00 | $0.00 | 20 |
| 7369 | SMITH | CLERK | 7902 | 12/17/1980 | $800.00 | $0.00 | 20 |
| 7566 | JONES | MANAGER | 7839 | 4/2/1981 | $0.00 | $0.00 | 20 |
| 7698 | BLAKE | MANAGER | 7839 | 5/1/1981 | $2,850.00 | $0.00 | 30 |
| 7499 | ALLEN | SALESMAN | 7698 | 2/20/1981 | $1,600.00 | $300.00 | 30 |
| 7844 | TURNER | SALESMAN | 7698 | 9/8/1981 | $1,500.00 | $0.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 2/22/1981 | $1,250.00 | $500.00 | 30 |
| 7654 | MARTIN | SALESMAN | 7698 | 9/28/1981 | $1,250.00 | $1,400.00 | 30 |
| 7900 | JAMES | CLERK | 7698 | 12/3/1981 | $950.00 | $0.00 | 30 |
+-------+--------+-----------+------+------------+-----------+-----------+------+
到目前为止,这是我的代码:
SELECT EMp.Mgr, EMp.Ename, EMp.Sal AS Sal
FROM EMp
GROUP BY EMp.Mgr, EMp.Ename, EMp.Sal
HAVING (((EMp.Mgr) Is Not Null) AND ((EMp.Sal)>1000))
ORDER BY EMp.Sal DESC;
我当前代码的问题在于它没有考虑最低工资参数。我认为这需要通过使用子查询来完成,但我完全确定如何继续使用...
有人可以帮忙吗?
答案 0 :(得分:1)
试试这个:
SELECT EMp.Mgr, EMp.Ename, EMp.Sal AS Sal
FROM EMp
WHERE emp.Sal = (select MIN(sal) from emp as emp2 where emp2.MGr = emp.Mgr and emp2.sal > 1000)
GROUP BY EMp.Mgr, EMp.Ename, EMp.Sal
HAVING EMp.Mgr Is Not Null
ORDER BY EMp.Sal DESC;
答案 1 :(得分:0)
请尝试
with one as
(
SELECT EMp.Mgr,min(EMp.Sal) MinSlary
FROM EMp
GROUP BY EMp.Mgr
)
select a.Mgr,b.EName,b.Sal from one a
inner join Emp b on a.Mgr=b.Mgr and a.MinSlary=b.Sal
where a.Mgr is not null and a.MinSlary>1000
答案 2 :(得分:0)
以下是基于杰拉德的回答,但是我在那里发表的评论中提到了对此的担忧:
SELECT Emp.Mgr, Emp.Ename, Emp.Sal AS Sal
FROM Emp
WHERE Emp.Sal=(SELECT MIN(sal)
FROM Emp as Emp2
WHERE Emp2.MGr = Emp.Mgr
HAVING min(Emp2.sal) >= 1000)
GROUP BY Emp.Mgr, Emp.Ename, Emp.Sal
HAVING Emp.Mgr Is Not Null
ORDER BY Emp.Sal DESC;
使用您的样本数据,返回与Jerrad相同的行,除了省略那些与经理7839或7698的行。除此之外,这两个管理员工的薪水为$ 0和$ 950。我解释原始问题的方式("排除最低工资低于1000美元和#34的任何群体),这些经理应从结果中排除。