简体   繁体   English

将两个SQL Select查询与where子句组合

[英]Combining Two SQL Select Queries with Where Clauses

I have two Oracle queries that I need combined through an inner join where the tables are joined using the person_uid field. 我有两个需要通过内部联接组合的Oracle查询,在内部联接中使用person_uid字段将表联接在一起。 This is because I need to compare what an employee's pay, job title, and supervisor was from one year to the next. 这是因为我需要比较员工从一年到下一年的薪水,职位和主管。 I need to have the 2015 data and the 2014 data in the same row for each employee, so if this can be done by doing a subquery using an inner join on the person_uid field, that is the method that I believe will accomplish this. 我需要为每位员工在同一行中包含2015年数据和2014年数据,因此,如果可以通过使用person_uid字段上的内部联接进行子查询来完成此操作,那么我相信这是可以实现的方法。

Here is the first query that pulls the necessary 2015 data: 这是提取必要的2015年数据的第一个查询:

SELECT  person_uid,
    id ,
    position_contract_type,
    position,
    job_suffix,
    position_status,
    effective_date,
    position_employee_class,
    timesheet_organization ,
    appointment_pct ,
    annual_salary ,
    per_pay_salary ,
    hourly_rate ,
    position_title ,
    academic_title ,
    supervisor_id ,
    supervisor_name ,
    supervisor_position ,
    supervisor_job_suffix ,
    supervisor_title ,
    assignment_grade ,
    position_change_reason ,
    position_change_reason_desc
FROM    employee_position_cunm posn
WHERE   posn.position_contract_type = 'P'
AND     posn.position_status <> 'T'
AND     posn.effective_date = (SELECT MAX(effective_date)
                           FROM   employee_position_cunm p2
                           WHERE  p2.person_uid = posn.person_uid
                           AND    p2.position = posn.position
                           AND    p2.job_suffix = posn.job_suffix
                           AND    p2.effective_date <= '01-Nov-2015')
order by person_uid

I need it to be joined to this query on the person_uid field so that each unique ID for the employee has the records for both years in a single row: 我需要将它连接到person_uid字段上的此查询中,以便该员工的每个唯一ID在一行中都包含两年的记录:

SELECT  person_uid,
    id ,
    position_contract_type,
    position,
    job_suffix,
    position_status,
    effective_date,
    position_employee_class,
    timesheet_organization ,
    appointment_pct ,
    annual_salary ,
    per_pay_salary ,
    hourly_rate ,
    position_title ,
    academic_title ,
    supervisor_id ,
    supervisor_name ,
    supervisor_position ,
    supervisor_job_suffix ,
    supervisor_title ,
    assignment_grade ,
    position_change_reason ,
    position_change_reason_desc
FROM    employee_position_cunm posn
WHERE   posn.position_contract_type = 'P'
AND     posn.position_status <> 'T'
AND     posn.effective_date = (SELECT MAX(effective_date)
                           FROM   employee_position_cunm p2
                           WHERE  p2.person_uid = posn.person_uid
                           AND    p2.position = posn.position
                           AND    p2.job_suffix = posn.job_suffix
                           AND    p2.effective_date <= '01-Nov-2014')
order by person_uid

An easy way would be to use OR : 一种简单的方法是使用OR

WHERE posn.position_contract_type = 'P' AND
      posn.position_status <> 'T' AND
      (posn.effective_date = (SELECT MAX(effective_date)
                              FROM   employee_position_cunm p2
                              WHERE  p2.person_uid = posn.person_uid
                                     p2.position = posn.position AND
                                     p2.job_suffix = posn.job_suffix AND
                                     p2.effective_date <= '01-Nov-2014'
                             ) OR
       posn.effective_date = (SELECT MAX(effective_date)
                              FROM   employee_position_cunm p2
                              WHERE  p2.person_uid = posn.person_uid
                                     p2.position = posn.position AND
                                     p2.job_suffix = posn.job_suffix AND
                                     p2.effective_date <= '01-Nov-2015'
                             )
      )

In Oracle you could do a UNION or a UNION ALL . 在Oracle中,您可以执行UNIONUNION ALL

SELECT  person_uid,
    id ,
    position_contract_type,
    position,
    job_suffix,
    position_status,
    effective_date,
    position_employee_class,
    timesheet_organization ,
    appointment_pct ,
    annual_salary ,
    per_pay_salary ,
    hourly_rate ,
    position_title ,
    academic_title ,
    supervisor_id ,
    supervisor_name ,
    supervisor_position ,
    supervisor_job_suffix ,
    supervisor_title ,
    assignment_grade ,
    position_change_reason ,
    position_change_reason_desc
FROM    employee_position_cunm posn
WHERE ...
...
...
UNION ALL
SELECT  person_uid,
    id ,
    position_contract_type,
    position,
    job_suffix,
    position_status,
    effective_date,
    position_employee_class,
    timesheet_organization ,
    appointment_pct ,
    annual_salary ,
    per_pay_salary ,
    hourly_rate ,
    position_title ,
    academic_title ,
    supervisor_id ,
    supervisor_name ,
    supervisor_position ,
    supervisor_job_suffix ,
    supervisor_title ,
    assignment_grade ,
    position_change_reason ,
    position_change_reason_desc
FROM    employee_position_cunm posn
WHERE ....
....
....;

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

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