将两个SQL Select查询与Where子句结合使用

时间:2015-10-29 21:53:14

标签: mysql sql oracle

我需要通过内连接组合两个Oracle查询,其中使用person_uid字段连接表。这是因为我需要比较员工的工资,职称和主管从一年到下一年的情况。我需要为每个员工在同一行中提供2015年数据和2014年数据,因此如果可以通过在person_uid字段上使用内部联接执行子查询来完成此操作,那么我认为这将实现此目的。< / p>

以下是第一个提取必要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

我需要在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

2 个答案:

答案 0 :(得分:0)

一种简单的方法是使用Code igniter 2: (Notice the in the 2nd one there is NOT a "+" sign) string(58) "1bcw1kHHf0yTi8N%2bQGpdvsn4i7y3p2h1YAJnKy1QMS4iEgUQESIQEnSl" string(56) "1bcw1kHHf0yTi8N QGpdvsn4i7y3p2h1YAJnKy1QMS4iEgUQESIQEnSl" string(56) "1bcw1kHHf0yTi8N+QGpdvsn4i7y3p2h1YAJnKy1QMS4iEgUQESIQEnSl" Code igniter 3: (Works as expected) string(58) "1bcw1kHHf0yTi8N%2bQGpdvsn4i7y3p2h1YAJnKy1QMS4iEgUQESIQEnSl" string(56) "1bcw1kHHf0yTi8N+QGpdvsn4i7y3p2h1YAJnKy1QMS4iEgUQESIQEnSl" string(56) "1bcw1kHHf0yTi8N+QGpdvsn4i7y3p2h1YAJnKy1QMS4iEgUQESIQEnSl"

OR

答案 1 :(得分:0)

在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 ....
....
....;