oracle:条展子查询两个表

发布于 2025-02-04 06:01:52 字数 1344 浏览 2 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

硬不硬你别怂 2025-02-11 06:01:53

带有子句的SQL创建“虚拟”表,您可以在该子句下的以后查询中引用。这些表中存在记忆中查询的寿命。它们有点像视图。

当您建立一个称为find_emp的虚拟表时,您的示例无法正常工作,但是您没有使用它。

一个示例

WITH subquery AS (
SELECT col1 , col2, col3 
FROM table1
WHERE col4=condition1
)

SELECT * FROM subquery;

希望有所帮助。

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

WITH subquery AS (
SELECT col1 , col2, col3 
FROM table1
WHERE col4=condition1
)

SELECT * FROM subquery;

Hope that helps.

随风而去 2025-02-11 06:01:53

如果您正在寻找一名具有相同工作的员工,这意味着该员工的Job_history表中只有一个条目。

因此,您可以做这样的事情。


CREATE table dept  (department_id, department_name) AS
SELECT 1, 'IT' FROM DUAL UNION ALL
SELECT 2, 'MARKETING' FROM DUAL UNION ALL
SELECT 3, 'SALES'  FROM DUAL;

CREATE TABLE employees (employee_id, first_name, last_name, department_id, sal) AS
SELECT 1, 'Alice', 'Abbot', 1, 100000 FROM DUAL UNION ALL
SELECT 2, 'Beryl', 'Baron', 1, 50000 FROM DUAL;

CREATE table job_history  (employee_id,
department_id) AS
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 2, 3   FROM DUAL;

WITH rws AS
(
  SELECT 
   e.employee_id,
   e.first_name, 
   e.last_name,
   e.department_id,
   d.department_name
 FROM EMPLOYEES e join dept d on e.department_id = d.department_id
WHERE EMPLOYEE_ID IN
  (SELECT EMPLOYEE_ID FROM JOB_HISTORY GROUP BY EMPLOYEE_ID  HAVING COUNT(EMPLOYEE_ID) =1)
)
SELECT 
   employee_id,
   first_name, 
   last_name,
   department_id,
   department_name
 FROM rws;

EMPLOYEE_ID    FIRST_NAME    LAST_NAME    DEPARTMENT_ID    DEPARTMENT_NAME
1    Alice    Abbot    1    IT

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.


CREATE table dept  (department_id, department_name) AS
SELECT 1, 'IT' FROM DUAL UNION ALL
SELECT 2, 'MARKETING' FROM DUAL UNION ALL
SELECT 3, 'SALES'  FROM DUAL;

CREATE TABLE employees (employee_id, first_name, last_name, department_id, sal) AS
SELECT 1, 'Alice', 'Abbot', 1, 100000 FROM DUAL UNION ALL
SELECT 2, 'Beryl', 'Baron', 1, 50000 FROM DUAL;

CREATE table job_history  (employee_id,
department_id) AS
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 2, 3   FROM DUAL;

WITH rws AS
(
  SELECT 
   e.employee_id,
   e.first_name, 
   e.last_name,
   e.department_id,
   d.department_name
 FROM EMPLOYEES e join dept d on e.department_id = d.department_id
WHERE EMPLOYEE_ID IN
  (SELECT EMPLOYEE_ID FROM JOB_HISTORY GROUP BY EMPLOYEE_ID  HAVING COUNT(EMPLOYEE_ID) =1)
)
SELECT 
   employee_id,
   first_name, 
   last_name,
   department_id,
   department_name
 FROM rws;

EMPLOYEE_ID    FIRST_NAME    LAST_NAME    DEPARTMENT_ID    DEPARTMENT_NAME
1    Alice    Abbot    1    IT

倾听心声的旋律 2025-02-11 06:01:52

应该是一条线:(

WITH start_dept AS
(
  SELECT emp.employee_id, dept.deparment_id AS prev_dep
    FROM employees   emp
       , job_history dept
   WHERE emp.employee_id = dept.employee_id
     AND emp.hire_date   = dept.start_date
)
SELECT e.employee_id, e.last_name, e.hire_date, e.deparment_id AS current_dep, sd.prev_dep
  FROM employees  e
     , start_dept sd
 WHERE e.employee_id  = sd.employee_id
   AND e.deparment_id = sd.prev_dep;

假设员工。deparment_id是当前的部门和员工。hire_date与job_history中的start_date匹配)

Should be something among the lines:

WITH start_dept AS
(
  SELECT emp.employee_id, dept.deparment_id AS prev_dep
    FROM employees   emp
       , job_history dept
   WHERE emp.employee_id = dept.employee_id
     AND emp.hire_date   = dept.start_date
)
SELECT e.employee_id, e.last_name, e.hire_date, e.deparment_id AS current_dep, sd.prev_dep
  FROM employees  e
     , start_dept sd
 WHERE e.employee_id  = sd.employee_id
   AND e.deparment_id = sd.prev_dep;

(assuming Employees.deparment_id is the current departmen and Employees.hire_date matches the start_date from Job_history)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文