我正在尝试编写SQL SELECT语句,以找出EACH部门中最近雇用的员工。我正在研究Oracle数据库,我正在尝试显示部门名称,员工姓名,雇用日期和薪水。员工和部门表的表定义如下 部门表
CREATE TABLE dpt
( DEPARTMENT_ID NUMBER(4) PRIMARY KEY,
DEPARTMENT_NAME VARCHAR2(20) NOT NULL,
ADDRESS VARCHAR2(20) NOT NULL);
示例数据
(10, 'ACCOUNTING', 'NEW YORK');
(20, 'RESEARCH', 'DALLAS');
(30, 'SALES', 'CHICAGO');
员工表
CREATE TABLE emp
( EMPLOYEE_ID NUMBER(4) PRIMARY KEY,
EMPLOYEE_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
MANAGER_ID NUMBER(4),HIRE_DATE DATE,
SALARY NUMBER(9, 2),
COMMISSION NUMBER(9, 2),
DEPARTMENT_ID NUMBER(4) REFERENCES dpt(DEPARTMENT_ID));
示例数据
(7839, 'KING', 'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
(7596, 'JOST', 'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
我写的查询仅在一个部门中输出雇佣最多的员工。有人可以指出我做错了什么吗?我尝试使用Group By但是无法以正确的方式执行此操作。
SELECT dpt.department_name, dpt.department_id, employee_name, hire_date, Salary
from dpt, emp
where emp.department_id = dpt.department_id
and
hire_date = (select max(hire_date) from emp where department_id = emp.department_id)
order by dpt.department_name;
答案 0 :(得分:1)
您缺少相关子查询中where
子句的表别名:
SELECT dpt.department_name, dpt.department_id, employee_name, hire_date, Salary
from dpt join
emp
on emp.department_id = dpt.department_id
where hire_date = (select max(hire_date) from emp emp2 where emp.department_id = emp2.department_id)
order by dpt.department_name;
基本上,条件where department_id = emp.department_id
从子查询中的表中提取两次列。它与外部查询无关。通过使用适当的表别名,您可以获得相关性。
我还更改了join
语法以使用显式连接形式。
答案 1 :(得分:1)
您可以通过加入
来完成SELECT dpt.department_name, dpt.department_id, employee_name, hire_date, Salary
from dpt inner join emp on emp.department_id = dpt.department_id inner join
(select emp.department_id, max(hire_date) as datemax from emp) x on emp.department_id=x.department_id and emp.hire_date =x.datemax
order by dpt.department_name;
答案 2 :(得分:1)
您可以尝试这样的事情:
SELECT d.department_name,
d.department_id,
e.employee_name,
e.hire_date,
e.salary
FROM dpt d,
JOIN (SELECT MAX(hire_date) over(PARTITION BY department_id) max_hdate,
emp.*
FROM emp) e ON e.department_id = d.department_id
WHERE e.hire_date = e.max_hdate;
答案 3 :(得分:0)
另一个解决方案就是这个:
with t as
(SELECT department_name, department_id, employee_name, hire_date, Salary,
max(hire_date) over (partition by department_id) as max_hire_date
from dpt join
join emp using (department_id))
select *
from t
where max_hire_date = hire_date
答案 4 :(得分:-1)
SELECT employee_id, employee_name, department_id FROM emp e1 WHERE hiredate = (SELECT max(hiredate) FROM emp e2 GROUP BY department_id HAVING e2.department_id=e1.department_id);
答案 5 :(得分:-1)
Data emp;
Input id name $ dept_no sal title $ hire_date;
informat hire_date date9.;
Cards;
555 Kearny 200 7000 Manager 08Feb2102
666 williams 100 8000 Manager 30Jan2104
668 Mitchele 100 5000 clerk 14Jul2015
777 Haberle 200 6000 Clerk 29Jan2001
888 Haberle 200 5000 Clerk 03Feb2015
890 Johnson 300 4000 Analyst 02Mar2012
889 Dupont 400 5000 clerk 06May2011 .
990 McCabe 400 6000 Programmer 10Dec2015
999 Lopez 300 9000 Manager 12dec2105
;
Data Dept;
Input Dept_no Dept_name $ Dept_city $ Dept_mgr;
cards;
100 Logistics Wichita 666
200 Finance Boise 555
300 Marketing Pierre 666
400 HR Paris 777
;
run;
proc sql;
select a.dept_no,c.dept_name,a.id,a.name,a.hire_date format=date9.
from emp a
inner join
(select dept_no,max(hire_date) as rec_date format=date9.
from emp group by 1) b
on a.dept_no=b.dept_no
inner join dept c
on a.dept_no=c.dept_no
where a.hire_date=b.rec_date
order by 5 desc;
quit;
dept_no Dept_name id name hire_date
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
300 Marketin 999 Lopez 12DEC2105
100 Logistic 666 williams 30JAN2104
200 Finance 555 Kearny 08FEB2102
400 HR 990 McCabe 10DEC2015