我的 sql 脚本遇到问题,该脚本要求我显示公司、员工编号、姓氏、名字、当前工作和当前工资。我知道要达到“当前”的某些内容意味着涉及日期。 我想要实现的是显示该员工当前的工作和工资。
>>这些是使用的表格和视图<<
注意:表格内的信息并非真实信息。我只是改变它,这样你仍然可以得到我指出的内容。
emp表
+-------+----------+-----------+
| empno | lastname | firstname |
+-------+----------+-----------+
| 111 | james | lebron |
| 222 | bryant | kobe |
| 333 | jordan | michael |
| 444 | curry | stephen |
+-------+----------+-----------+
部门工作视图
+------------+--------+-----------+------------+------------+
| company | empno | effdate | job | salary |
+------------+--------+-----------+------------+------------+
| lakers | 111 | 04/16/15 | assistant | 1500.00 |
| lakers | 111 | 02/02/16 | coach | 4000.00 |
| lakers | 222 | 07/22/17 | analyst | 3000.00 |
| bulls | 333 | 04/12/17 | secretary | 5000.00 |
| bulls | 333 | 07/28/18 | manager | 5000.00 |
| warriors | 444 | 10/04/19 | chef | 2000.00 |
+------------+--------+-----------+------------+------------+
我所做的是,我使用 Max() 函数来获取有效日期(effdate),以可能获得最新的工作和薪水。但即使在那之后,它仍然显示他过去的工作和薪水。我需要显示当前的工作和薪水。
SELECT z.company, em.empno, em.lastname, em.firstname, z.job, z.salary
FROM emp em
JOIN ( SELECT dj.company, dj.empno, dj.effdate, dj.job, dj.salary FROM dept_job dj
JOIN (SELECT company, empno, MAX(effdate) AS maxefffdate FROM dept_job
GROUP BY company, empno) pe ON dj.empno = pc.empno AND dj.effdate = pe.maxeffdate) z
ON em.empno = z.empno
ORDER BY company, empno;
错误结果:
+------------+--------+----------+-----------+------------+------------+
| company | empno | lastname | firstname | job | salary |
+------------+--------+----------+-----------+------------+------------+
| lakers | 111 | james | lebron | assistant | 1500.00 |
| lakers | 111 | james | lebron | coach | 4000.00 |
| lakers | 222 | bryant | kobe | analyst | 3000.00 |
| bulls | 333 | jordan | michael | secretary | 5000.00 |
| bulls | 333 | jordan | michael | manager | 5000.00 |
| warriors | 444 | curry | stephen | chef | 2000.00 |
+------------+--------+----------+-----------+------------+------------+
我想要达到的目标
+------------+--------+----------+-----------+------------+------------+
| company | empno | lastname | firstname | job | salary |
+------------+--------+----------+-----------+------------+------------+
| lakers | 111 | james | lebron | coach | 4000.00 |
| lakers | 222 | bryant | kobe | analyst | 3000.00 |
| bulls | 333 | jordan | michael | manager | 5000.00 |
| warriors | 444 | curry | stephen | chef | 2000.00 |
+------------+--------+----------+-----------+------------+------------+
您可以从下面的查询中获得所需的输出:
SELECT dj.company
,em.empno
,em.lastname
,em.firstname
,dj.job
,dj.salary
FROM emp em
JOIN dept_job dj ON em.empno = dj.empno
JOIN (
SELECT empno
,MAX(effdate) AS maxefffdate
FROM dept_job
GROUP BY empno
) z ON dj.empno = z.empno
AND dj.effdate = z.maxefffdate
ORDER BY company
,empno;
您可以将 OVER 与 PARTION BY 一起使用,并执行以下操作:
选择 z.company、em.empno、em.lastname、em.firstname、z.job、z.salary 来自 emp em JOIN ( 选择 dj.company, dj.empno, MAX(dj.effdate) OVER ( 按 dj.company、dj.empno、dj.job、dj.salary 分区 ) AS maxeffdate、dj.job、dj.salary 来自 dept_job dj ) z ON em.empno = z.empno 按公司、员工订购;