oracle:条展子查询两个表
使用Oracle数据库,我有两个表:
Employees:
Employee_id | Number(6,0)
Last_name | Varchar2(20)
Hire_date | Date
Deparment_id| Number(4,0)
Job_history:
Employee_id | Number(6,0)
Start_date | Date
Deparment_id | Number(4,0)
我应该找到 - 使用with -sause-当前在同一部门工作的所有员工开始工作(Hire_date = start_date和同一dectment_id)。我使用子查询中的加入很容易获得正确的结果:(
SELECT DISTINCT e.employee_id, e.last_name, e.hire_date,
e.department_id as current_dep, j.department_id as prev_dep
FROM hr.employees e
JOIN (SELECT employee_id, department_id, end_date, start_date
FROM hr.job_history ) j
ON e.employee_id = j.employee_id
WHERE e.department_id = j.department_id;
右)输出:
不幸的是,我不确定如何管理两个不同的表(大多数示例i在网络上发现的只是一张表)
--best try until now--
With find_emp as (SELECT hire_date, department_id
FROM hr.employees)
SELECT e.employee_id, e.last_name, e.department_id as curr_dep
FROM HR.employees e
WHERE e.hire_date IN (SELECT j.start_date
FROM hr.job_history j
JOIN hr.employees e
ON e.employee_id = j.employee_id);
(错误)输出:
我在做什么错?由于我是SQL的新手,因此我将感谢所有提示。非常感谢您。
Using an Oracle database, I have two tables:
Employees:
Employee_id | Number(6,0)
Last_name | Varchar2(20)
Hire_date | Date
Deparment_id| Number(4,0)
Job_history:
Employee_id | Number(6,0)
Start_date | Date
Deparment_id | Number(4,0)
I am supposed to find - using the WITH-clause - all the employees who currently work in the same department where they started to work (hire_date = start_date and same department_id). I easily got the right result using a JOIN in the subquery:
SELECT DISTINCT e.employee_id, e.last_name, e.hire_date,
e.department_id as current_dep, j.department_id as prev_dep
FROM hr.employees e
JOIN (SELECT employee_id, department_id, end_date, start_date
FROM hr.job_history ) j
ON e.employee_id = j.employee_id
WHERE e.department_id = j.department_id;
(Right) OUTPUT:
Unfortunately with the WITH-clause I am getting troubles, since I am not sure how to manage two different tables (most examples I found on the web are just with one table)
--best try until now--
With find_emp as (SELECT hire_date, department_id
FROM hr.employees)
SELECT e.employee_id, e.last_name, e.department_id as curr_dep
FROM HR.employees e
WHERE e.hire_date IN (SELECT j.start_date
FROM hr.job_history j
JOIN hr.employees e
ON e.employee_id = j.employee_id);
(wrong) OUTPUT:
What I am doing wrong? Since I am new to SQL, I would appreciate every hint. Thank you very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
带有子句的SQL创建“虚拟”表,您可以在该子句下的以后查询中引用。这些表中存在记忆中查询的寿命。它们有点像视图。
当您建立一个称为
find_emp
的虚拟表时,您的示例无法正常工作,但是您没有使用它。一个示例
希望有所帮助。
The SQL WITH clause creates 'virtual' tables which you can reference in later queries under the clause. These tables exist for the lifetime of the query in memory. They are a bit like views.
Your example is not working as you are establishing a virtual table called
find_emp
, but then you are not using it.An example
Hope that helps.
如果您正在寻找一名具有相同工作的员工,这意味着该员工的Job_history表中只有一个条目。
因此,您可以做这样的事情。
If you're looking for an employee that has the same job that means there will be only one entry in the JOB_HISTORY table for that employee.
Therefore, you can do something like this.
应该是一条线:(
假设员工。deparment_id是当前的部门和员工。hire_date与job_history中的start_date匹配)
Should be something among the lines:
(assuming Employees.deparment_id is the current departmen and Employees.hire_date matches the start_date from Job_history)