2012-02-22 47 views
0

好的,首先我會嘗試解釋我正在嘗試做什麼。我正在嘗試構建一份報告,以返回分配給客戶的所有評估。每個評估都應該有相應的事件,反之亦然(不幸的是,兩者之間沒有令牌傳遞,所以我必須根據日期確定正確的事件)。但是,事件有時會丟失,或者評估有時會丟失。我希望掌握所有這些信息,以便我可以報告缺少評估或事件的地方並修復它們。這是最簡單的形式,這是我期望報告的樣子。但是,還有其他一些條件,例如返回特定類型的評估和特定類型的事件。Oracle SQL加入兩個類似的查詢

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的情況有關,這是爲了協助將相應的評估返回到事件,反之亦然,但是我認爲當試圖加入這兩個時,這可能會導致一些事件問題?無論如何,感謝您的時間 - 我會繼續鼓搗,看看我能否找到解決方案,但任何提示或建議將不勝感激。

回答

1

你爲什麼不聯合的2個查詢?如果適用,請使用工會全部這是很多執行者,工會

+0

一個工會似乎對我來說比完整的外部聯盟更好,有一些調整,它看起來像我想要的那樣工作,所以謝謝! – bawpie 2012-02-27 11:07:03

1

你嘗試過一個完整的外部連接? http://www.oreillynet.com/network/2002/04/23/fulljoin.html

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 
+0

想想我嘗試了一個完整的外連接,但不認爲它可以工作,即我收到了一個事件和一個評估事件的評估,但沒有發現它們丟失的實例。我認爲行號= 1的條件可能會讓它崩潰,或者它可能是我加入的措辭。將有一個遊戲,並取得成果,謝謝。 – bawpie 2012-02-22 21:56:53

+0

似乎加入查詢不是複雜的部分,但返回相關的相應信息是 - 使用完全外連接肯定似乎工作,所以謝謝! – bawpie 2012-02-23 12:32:15

+0

在試圖解決這個問題之後,我不確定完整的外連接是否正常工作,因爲我想 - 我得到很多沒有正確相應日期的重複行。我認爲這取決於用於確定相應日期的行數,而我毫不懷疑完整的外連接也可以做到這一點,我已經設法使用union all來工作。 – bawpie 2012-02-27 11:07:51

1

像BD表示,一個完整的OUTER JOIN會做的伎倆,但UNION也可能是一個好主意