如何添加两个复杂的 SQL 查询并在单个查询中获取输出?
How to add two complex SQL Queries and get output in single query?
我是编码新手,正在准备 SQL 命令,但卡在了一步。下面是主要的 SQL 查询:
select
distinct
hap.position_code as RequisitionNumber,
COALESCE(hap.attribute10,'UNIVERSAL') as RequisitionType, hap.name as
RequisitionTitle,
paam1.job_id as RequisitionTemplate, COALESCE(hap.attribute11,'1') as
NumberToHire,
pu.username as HiringManagerLogin,
paam1.job_id as JobField,
hap.full_part_time as Schedules, hap.position_code as PositionCode
from hr_all_positions hap, per_all_assignments_m paam1, per_users pu
where hap.position_id = paam1.position_id
and hap.position_id in
(
select distinct paam.position_id from per_all_assignments_m paam,
per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate
)
在上面的查询中,我从下面附加的另一个查询中获取 pu.username 的值:
select pu.username from per_users pu where person_id = (
select pmhd.manager_id from PER_MANAGER_HRCHY_DN pmhd,
per_periods_of_service ppos
where pmhd.person_id = ppos.person_id and pmhd.IMMEDIATE_REPORTEE_ASG_ID is
not null
and pmhd.EFFECTIVE_END_DATE = ppos.actual_termination_date and
ppos.actual_termination_date >= sysdate
and pmhd.person_id = (select distinct paam.person_id from
per_all_assignments_m paam, per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate)
)
现在,当我像下面这样加入两个查询时:
select
distinct
hap.position_code as RequisitionNumber,
COALESCE(hap.attribute10,'UNIVERSAL') as
RequisitionType, hap.name as RequisitionTitle,
paam1.job_id as RequisitionTemplate, COALESCE(hap.attribute11,'1') as
NumberToHire,
pu.username as HiringManagerLogin,
paam1.job_id as JobField,
hap.full_part_time as Schedules, hap.position_code as PositionCode
from hr_all_positions hap, per_all_assignments_m paam1, per_users pu
where hap.position_id = paam1.position_id and paam1.person_id = pu.person_id
and pu.person_id = (
select pmhd.manager_id from PER_MANAGER_HRCHY_DN pmhd, per_periods_of_service
ppos
where pmhd.person_id = ppos.person_id and pmhd.IMMEDIATE_REPORTEE_ASG_ID is
not null
and pmhd.EFFECTIVE_END_DATE = ppos.actual_termination_date and
ppos.actual_termination_date >= sysdate
and pmhd.person_id = (select distinct paam.person_id from
per_all_assignments_m
paam, per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate)
)
and hap.position_id in
(
select distinct paam.position_id from per_all_assignments_m paam,
per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate
)
我正在尝试这是 Oracle 数据库
没有数据输出,我不明白为什么?
请帮助我!!
提前致谢,
希瓦姆
老实说,您的最终查询输出看起来像是其他两者的适当组合。但是,由于附加的连接条件,您可能得不到任何输出:
paam1.person_id = pu.person_id
这不在您的任何一个原始查询中,并且每一行都可能无法满足此连接条件。尝试删除它以查看是否输出任何行。尝试将其添加到您的第一个查询中。如果输出行,那么这可能意味着您在 pu.person_id 上的附加过滤器非常有选择性。
我是编码新手,正在准备 SQL 命令,但卡在了一步。下面是主要的 SQL 查询:
select
distinct
hap.position_code as RequisitionNumber,
COALESCE(hap.attribute10,'UNIVERSAL') as RequisitionType, hap.name as
RequisitionTitle,
paam1.job_id as RequisitionTemplate, COALESCE(hap.attribute11,'1') as
NumberToHire,
pu.username as HiringManagerLogin,
paam1.job_id as JobField,
hap.full_part_time as Schedules, hap.position_code as PositionCode
from hr_all_positions hap, per_all_assignments_m paam1, per_users pu
where hap.position_id = paam1.position_id
and hap.position_id in
(
select distinct paam.position_id from per_all_assignments_m paam,
per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate
)
在上面的查询中,我从下面附加的另一个查询中获取 pu.username 的值:
select pu.username from per_users pu where person_id = (
select pmhd.manager_id from PER_MANAGER_HRCHY_DN pmhd,
per_periods_of_service ppos
where pmhd.person_id = ppos.person_id and pmhd.IMMEDIATE_REPORTEE_ASG_ID is
not null
and pmhd.EFFECTIVE_END_DATE = ppos.actual_termination_date and
ppos.actual_termination_date >= sysdate
and pmhd.person_id = (select distinct paam.person_id from
per_all_assignments_m paam, per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate)
)
现在,当我像下面这样加入两个查询时:
select
distinct
hap.position_code as RequisitionNumber,
COALESCE(hap.attribute10,'UNIVERSAL') as
RequisitionType, hap.name as RequisitionTitle,
paam1.job_id as RequisitionTemplate, COALESCE(hap.attribute11,'1') as
NumberToHire,
pu.username as HiringManagerLogin,
paam1.job_id as JobField,
hap.full_part_time as Schedules, hap.position_code as PositionCode
from hr_all_positions hap, per_all_assignments_m paam1, per_users pu
where hap.position_id = paam1.position_id and paam1.person_id = pu.person_id
and pu.person_id = (
select pmhd.manager_id from PER_MANAGER_HRCHY_DN pmhd, per_periods_of_service
ppos
where pmhd.person_id = ppos.person_id and pmhd.IMMEDIATE_REPORTEE_ASG_ID is
not null
and pmhd.EFFECTIVE_END_DATE = ppos.actual_termination_date and
ppos.actual_termination_date >= sysdate
and pmhd.person_id = (select distinct paam.person_id from
per_all_assignments_m
paam, per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate)
)
and hap.position_id in
(
select distinct paam.position_id from per_all_assignments_m paam,
per_periods_of_service ppos
where paam.person_id = ppos.person_id and paam.position_id is not null and
ppos.actual_termination_date >= sysdate
)
我正在尝试这是 Oracle 数据库 没有数据输出,我不明白为什么?
请帮助我!!
提前致谢, 希瓦姆
老实说,您的最终查询输出看起来像是其他两者的适当组合。但是,由于附加的连接条件,您可能得不到任何输出:
paam1.person_id = pu.person_id
这不在您的任何一个原始查询中,并且每一行都可能无法满足此连接条件。尝试删除它以查看是否输出任何行。尝试将其添加到您的第一个查询中。如果输出行,那么这可能意味着您在 pu.person_id 上的附加过滤器非常有选择性。