简体   繁体   中英

Combining two Oracle SQL queries into one

I need to combine 2 query conditions in one SQL statement. I am unable to logically combine them.

1) Employee whose leave is approved by manager

select e.employee_name,r.request_from_Date,r.request_to_Date from  employee_leave e,emp_leave_request r
 where e.employee_id=r.request_from_id and e.employee_manager_id= r.request_to_id
 and r.request_Approved_date is not null
 and r.request_reject_Date is null
 and r.request_cancelled_Date is null;

2) Employee who is part of manager's team

 select employee_id, employee_name, employee_email, employee_username, employee_leave_normal, employee_contact_no,employee_designation
from employee_leave a
where exists(select 1 from employee_leave b where lower(b.employee_username)=lower(:APP_USER) and a.employee_manager_id=b.employee_id);

Use union to combine 2 sql select queries example: https://www.w3schools.com/sql/sql_union.asp

You can use INNER JOIN to achieve the same.Please learn what is an INNER join here

        SELECT     e1.*,
               e.request_from_date,
               e.request_to_date
    FROM       (
                      SELECT e.employee_id,
                             e.employee_name,
                             r.request_from_date,
                             r.request_to_date
                      FROM   employee_leave e,
                             emp_leave_request r
                      WHERE  e.employee_id=r.request_from_id
                      AND    e.employee_manager_id= r.request_to_id
                      AND    r.request_approved_date IS NOT NULL
                      AND    r.request_reject_date IS NULL
                      AND    r.request_cancelled_date IS NULL)e
    inner join
               (
                      SELECT employee_id,
                             employee_name,
                             employee_email,
                             employee_username,
                             employee_leave_normal,
                             employee_contact_no,
                             employee_designation
                      FROM   employee_leave a
                      WHERE  EXISTS
                             (
                                    SELECT 1
                                    FROM   employee_leave b
                                    WHERE  Lower(b.employee_username)=Lower(:APP_USER)
                                    AND    a.employee_manager_id=b.employee_id) e1
                      ON e.employee_id=e1.employee_id 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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