|
这是继续之前解决的查询(到目前为止最近的日期已经从不同的表中返回),但我现在希望进一步开发。
) H" [! B4 t( H& q) P) x( C我的 SQL 是这样的:
0 k! d8 D7 _" w6 c1 vSELECT *FROM (SELECT O_ASSESSMENTS.ASM_SUBJECT_ID as " Number", O_ASSESSMENTS.ASM_ID as "Assessment ID", O_ASSESSMENTS.ASM_START_DATE as "Assessment Start", O_ASSESSMENTS.ASM_END_DATE as "Assessment End", O_SERVICE_EVENTS.SEV_ID as "Event ID", O_SERVICE_EVENTS.SEV_ACTUAL_DATE as "Event Start", O_SERVICE_EVENTS.SEV_OUTCOME_DATE as "Event End", ROUND(O_ASSESSMENTS.ASM_START_DATE -O_SERVICE_EVENTS.SEV_ACTUAL_DATE,0) as "Likely", row_number() over(PARTITION BY O_ASSESSMENTS.ASM_ID ORDER BY abs(O_ASSESSMENTS.ASM_START_DATE - O_SERVICE_EVENTS.SEV_ACTUAL_DATE))as "Row Number" FROM O_ASSESSMENTS JOIN O_SERVICE_EVENTS ON O_ASSESSMENTS.ASM_SUBJECT_ID = O_SERVICE_EVENTS.SEV_SUBJECT_ID Where O_SERVICE_EVENTS.SEV_CODE IN ('ICS_E3','CPINVEST') AND O_ASSESSMENTS.ASM_QSA_ID IN ('AA1329','AA521') )WHERE "Row Number" = 1基本上我们有两个表 - o_assessments 和 o_service_events,该 SQL 将最近的服务事件返回评估。现在我想在查询中包含更多的上下文信息,以便对最终用户更有帮助 - 即团队名称和工作人员名称。0 N$ x U6 m* d0 p
不幸的是,团队和工作人员在不同的表中(_并通过 asm_id 到 res_rec_i链接到o_assessments。( ]! c! R, \" Y5 S; u- a
问题是,我真的不知道如何将这个表调用到上面 sql 中 -因此,任何建议都不会感激!
7 @ T4 j6 t% q3 M( T R我还想知道现有查询是否可以调整,当可能字段的差异为 0 时返回相应的事件(因为更大的值可能与特定评估无关)。我知道我可以把它添加到 where,但如果我加上这个行
, W K; i! G0 ?6 c4 v8 B# }) Hand "Likely" = 0它只返回事件评估,不会突出任何问题(即没有相应事件评估)。0 y6 H- x4 t6 k+ d. V! r
我一直在学习 SQL,但是有很多事情似乎真的超出了我的范围,所以我非常感谢任何建议!我不确定是修改我的原始查询还是开始新的查询,所以我希望我不会无意中违反任何规则。2 P/ l. r/ Y" X1 ]! {+ I; }
编辑:
* o. X3 V4 N5 |/ `6 t好的,按照马克的解决方案,这是我的工作。
3 D$ t: a& m+ Y; y! u$ o3 J% WSELECT * FROM (SELECT OAS.ASM_SUBJECT_ID as " Number",OAS.ASM_ID as "Assessment ID",OAS.ASM_START_DATE as "Assessment Start",OAS.ASM_END_DATE as "Assessment End",OAS.ASM_AUTH_DATETIME as "Authorisation Date",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",nvl(olm_bo.get_per_name(ORE.RES_PARTY_ID),'') as "Worker",OSE.SEV_ID as "Event ID",OSE.SEV_ACTUAL_DATE as "Event Start",OSE.SEV_OUTCOME_DATE as "Event End",ROUND(OAS.ASM_START_DATE -OSE.SEV_ACTUAL_DATE,0) as "Likely",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_IDAND 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 ('EVENT') AND ROUND(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE,0) >= -7ANDROUND(OAS.ASM_START_DATE - OSE.SEV_ACTUAL_DATE,0) 'Abandon' ) WHERE "Row Number" =
Y8 W1 O& ^( m) ^! U 解决方案: |
|