好的,首先,我将尝试解释我正在尝试做什么。我正在尝试构建一个报告,返回分配给客户的所有评估。每个评估都应该有相应的事件,反之亦然(遗憾的是,两者之间没有令牌传递,所以我必须根据日期确定正确的事件)。但是,有时会遗漏该事件,或者有时会遗漏评估。我希望捕获所有这些内容,以便我可以报告缺少评估或事件的位置,并将其修复。在这是最简单的形式,这是我期望报告的样子。但是,还有一些其他条件,例如返回特定类型的评估和特定类型的事件。
ID Assessment Event
1 A1 B1
2 A2
3 B3
我目前有2个查询,向我显示所有评估及其相应的事件(如果有的话),以及所有事件及其相应的评估(如果有的话)。它们似乎都运行良好,但我真的想加入这两个查询(ID在两个查询中都很常见)。我自己试过这个,但它从来都不行,所以我希望如果我发布我的两个工作查询,有人可能会提供帮助!
这会带来与其相应事件(如果存在)的评估。
SELECT
"CLIENTID",
"Name",
"ASSESSMENT_ID",
"Assessment_Start",
"Assessment_End",
"Days open",
"Authorised",
"Worker",
"Event_ID",
"Event_Start"
FROM
(SELECT
OAS.ASM_SUBJECT_ID as "CLIENTID",
nvl(olm_bo.get_name(OAS.ASM_SUBJECT_ID,OAS.ASM_SUBJECT_IND),'') as "Name",
OAS.ASM_ID as ASSESSMENT_ID,
olm_bo.working_days(OAS.ASM_START_DATE, OAS.ASM_END_DATE) -1 as "Days open",
OAS.ASM_AUTH_DATETIME as "Authorised",
OAS.ASM_START_DATE as "Assessment_Start",
OAS.ASM_END_DATE as "Assessment_End",
nvl(olm_bo.get_ref_desc(OAS.ASM_OUTCOME,'ASM_OUTCOME'),'') as "Outcome",
nvl(olm_bo.get_org_name(ORE.RES_PARTY_OUN_ID),'') as "Team",
initcap(nvl(olm_bo.get_per_name(ORE.RES_PARTY_ID),'')) as "Worker",
OSE.SEV_ID as "Event_ID",
to_char(trunc(OSE.SEV_ACTUAL_DATE),'DD/MM/YYYY') as "Event_Start",
OSE.SEV_OUTCOME_DATE as "Event_End",
nvl(decode(OSE.SEV_CLASS,'CPINVEST',olm_bo.get_ref_desc (OSE.SEV_REASON_CODE,'CP_JUSTIFICATION'),olm_bo.get_ref_desc (OSE.SEV_REASON_CODE,'REASON_CODE')),'') as "Event Outcome",
row_number() over(PARTITION BY OAS.ASM_ID
ORDER BY
abs(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE))as "Row Number"
FROM O_ASSESSMENTS OAS
INNER JOIN O_RESPONSIBILITIES ORE ON OAS.ASM_ID = ORE.RES_REC_ID
AND nvl(olm_bo.get_org_name(ORE.RES_PARTY_OUN_ID),'') = '#Team#'
LEFT JOIN O_SERVICE_EVENTS OSE ON OAS.ASM_SUBJECT_ID = OSE.SEV_SUBJECT_ID
AND
OSE.SEV_CODE IN ('INI')
AND
ROUND(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE,0) >= -7
AND
ROUND(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE,0) <= 7
Where OAS.ASM_QSA_ID IN ('A1','A2')
AND
nvl(olm_bo.get_ref_desc(OAS.ASM_OUTCOME,'ASM_OUTCOME'),'NULL') NOT IN ( 'Abandon' )
AND
(
OAS.ASM_END_DATE Is Null
OR
OAS.ASM_AUTH_DATETIME Is Null
)
) WHERE "Row Number" = 1
这会带来相应评估的事件(如果存在)。
SELECT
"CLIENTID",
"Name",
"Event_ID",
"Event_Start",
"Event_End",
"Days_open",
"Worker",
"Team",
"Assessment_ID",
"Assessment_Start",
"Assessment_End",
FROM
(SELECT
OSE.SEV_SUBJECT_ID as "CLIENTID",
nvl(olm_bo.get_name(OSE.SEV_SUBJECT_ID,OSE.SEV_SUBJECT_IND),'') as "Name",
OAS.ASM_ID as "Assessment_ID",
olm_bo.working_days(OSE.SEV_ACTUAL_DATE, OSE.SEV_OUTCOME_DATE) -1 as "Days_open",
OAS.ASM_START_DATE as "Assessment_Start",
OAS.ASM_END_DATE as "Assessment_End",
olm_bo.get_org_name(OSE.SEV_OUN_SHORT_NAME) as "Team",
olm_bo.get_event_worker(OSE.SEV_ID,'NAME') as "Worker",
OSE.SEV_ID as "Event_ID",
to_char(trunc(OSE.SEV_ACTUAL_DATE),'DD/MM/YYYY') as "Event_Start",
OSE.SEV_OUTCOME_DATE as "Event_End",
nvl(decode(OSE.SEV_CLASS,'CPINVEST',olm_bo.get_ref_desc (OSE.SEV_REASON_CODE,'CP_JUSTIFICATION'),olm_bo.get_ref_desc(OSE.SEV_REASON_CODE,'REASON_CODE')),'') as "Event Outcome",
row_number() over(PARTITION BY OSE.SEV_SUBJECT_ID
ORDER BY
abs(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE))as "Row Number"
FROM O_SERVICE_EVENTS OSE
LEFT JOIN O_ASSESSMENTS OAS ON OAS.ASM_SUBJECT_ID = OSE.SEV_SUBJECT_ID AND nvl (olm_bo.get_ref_desc(OAS.ASM_OUTCOME,'ASM_OUTCOME'),'NULL') NOT IN ( 'Abandon' )
AND
OAS.ASM_QSA_ID IN ('A1','A2')
AND
ROUND(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE,0) >= -7
AND
ROUND(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE,0) <= 7
Where OSE.SEV_CODE IN ('INITASS')
AND
olm_bo.get_org_name(OSE.SEV_OUN_SHORT_NAME) = '#Team#'
AND
(
OSE.SEV_OUTCOME_DATE Is Null
)
) WHERE "Row Number" = 1
还有一些联合报告的捕获 - 事件和评估可以有不同的团队或工作人员,因此我可能需要从评估团队和事件工作者定义单独的事件团队和事件工作者。我确实认为这可以通过外部联接来实现,或者通过将评估和事件连接到一个单独的表来实现,该表返回所有客户的评估或事件,但如上所述,结果似乎从未如此相加单独的查询。我认为这可能与行号= 1条件有关,这可以帮助将相应的评估返回给事件,反之亦然,但我认为当试图加入这两个时,这可能会导致一些问题?无论如何,感谢您的时间 - 我将继续修补,看看我是否能找到解决方案,但任何提示或建议将不胜感激。
答案 0 :(得分:1)
SQL> select p.part_id, s.supplier_name
2 from part p full outer join supplier s
3 on p.supplier_id = s.supplier_id;
PART SUPPLIER_NAME
---- --------------------
P1 Supplier#1
P2 Supplier#2
P4
P3
Supplier#3
答案 1 :(得分:1)
就像BD说的那样,FULL OUTER JOIN可以解决问题,但是UNION也可能是一个好主意
答案 2 :(得分:1)
为什么不联合你的2个查询?如果适用,请使用 union all ,这是 union 的表现。