简体   繁体   English

如何将两个工作SELECT查询合并为一个查询?

[英]How do I combine two working SELECT queries into one query?

I have two queries which work independently of one another; 我有两个相互独立的查询;

SELECT e.employee_id, 
       e.first_name, 
       e.last_name, 
       e.job_id, 
        e.salary, 
        e.commission_pct, e.manager_id,  
       e.department_id,  
       j.start_date,  
       j.end_date
FROM hr.employees e
LEFT OUTER JOIN hr.job_history j 
    ON e.employee_id = j.employee_id
WHERE commission_pct IS NULL

This first one recalls the information from two different tables, hr.employees and hr.job_history . 第一个回忆来自两个不同表的信息, hr.employeeshr.job_history

Here is the second; 这是第二个;

SELECT e.employee_id, 
        e.last_name, 
        m.employee_id "MgrNo",  
       m.last_name "MgrName"
FROM hr.employees e 
INNER JOIN hr.employees m ON e.manager_id=m.employee_id

This is to link employee_id with manager_id to display each employee's manager surname. 这是为了将employee_idmanager_id以显示每个员工的经理姓氏。 However, when I try to include the two together I keep getting an error telling me I have an invalid prefix. 但是,当我尝试将两者结合在一起时,我不断收到错误,告诉我我的前缀无效。 Any ideas? 有任何想法吗?

Try: 尝试:

WITH temptable AS (
SELECT e.employee_id, e.last_name, m.employee_id "MgrNo", m.last_name "MgrName"
FROM hr.employees e 
INNER JOIN hr.employees m ON 
e.manager_id=m.employee_id)
SELECT e.employee_id, e.first_name, e.last_name, e.job_id, e.salary, e.commission_pct, e.manager_id, e.department_id, j.start_date, j.end_date, t.MgrNo, t.MgrName
FROM hr.employees e
LEFT OUTER JOIN hr.job_history j ON e.employee_id = j.employee_id
LEFT JOIN temptable t ON t.employee_id = e.employee_id
WHERE commission_pct IS NULL

This uses one as a subquery then joins its key on your first query's key. 这使用一个作为子查询,然后在第一个查询的键上加入其键。

It would seem that you are attempting to create a query which will produce employee salary/commission history with their manager info. 您似乎正在尝试创建一个查询,该查询将使用其经理信息生成员工薪资/佣金历史记录。 You can combine the queries like this: 您可以像这样组合查询:

SELECT e.employee_id, e.first_name, e.last_name, e.job_id, e.salary,
       commission_pct, e.manager_id, e.department_id, j.start_date, j.end_date, 
       m.employee_id "MgrNo", m.last_name "MgrName"
FROM hr.employees e
INNER JOIN hr.employees m ON e.manager_id=m.employee_id
LEFT OUTER JOIN hr.job_history j ON e.employee_id = j.employee_id
WHERE commission_pct IS NULL

"I keep getting an error telling me I have an invalid prefix." “我一直收到一个错误,告诉我我的前缀无效。”

That is a typo. 这是一个错字。 We can't spot it in a version of the query you haven't posted. 我们无法在您尚未发布的查询版本中发现它。 But basically you just need another join in the first query like this: 但基本上你只需要第一个查询中的另一个连接,如下所示:

SELECT e.employee_id, 
       e.first_name, 
       e.last_name, 
       e.job_id, 
       e.salary,                                     
       e.commission_pct, 
       m.employee_id "MgrNo", 
       m.last_name "MgrName",
       e.department_id, 
       j.start_date, 
       j.end_date
FROM hr.employees e
LEFT OUTER JOIN hr.job_history j 
   ON e.employee_id = j.employee_id
LEFT OUTER JOIN hr.employees m
   ON m.employee_id = e.manager_id
WHERE e.commission_pct IS NULL  

I suggest using an outer join, because normally not every employee has a manager (depending on how you have implemented the hierarchy). 我建议使用外连接,因为通常不是每个员工都有一个经理(取决于你如何实现层次结构)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM