2012-03-15 44 views
3

我遇到以下問題。我目前正試圖從我們的數據庫中的許多不同的表中返回信息。這個過程是一個活動被添加到系統中,理論上應該遵循某種類型的協議。但是,有時這項活動沒有遵循預期的協議,我想確定情況如何。有關加入連接的ORACLE SQL查詢

我現在有一個查詢(如下圖所示),它返回它看起來像這樣的數據(從實際的查詢簡體):

ID Activity Agreement  Agreement Type 
1 X   Budgets  Payment 
2 X 
3 X   Budgets 

哪些數據應該看起來像如下:

ID Activity Agreement  Agreement Type 
1 X   Budgets  Payment 
2 X 
3 X   

基本上,如果沒有協議類型的付款,我不希望協議顯示(但我仍然希望看到該活動)。 ID用於將活動加入到協議中,但協議和協議類型來自加入描述ID的單獨表格。

SELECT O_ACTIVITIES.ACT_SUBJECT_ID as "ID", 
     initcap(olm_bo.get_per_name(O_ACTIVITIES.ACT_SUBJECT_ID)) as "Name", 
     O_ACTIVITIES.ACT_ID as "Activity ID", 
     initcap(O_ACTIVITY_TYPES.ACT_DESC) as "Activity Type", 
     O_ACTIVITIES.ACT_REQUESTED_DATE as "Start Date", 
     case when olm_bo.get_ref_desc(O_ACTIVITIES.ACT_STATUS,'ACTIVITY_STATUS') = 'Newly generated' 
        then null 
        else O_ACTIVITIES.ACT_STATUS_DATE 
      end as "End Date", 
     olm_bo.get_ref_desc(O_ACTIVITIES.ACT_STATUS,'ACTIVITY_STATUS') as "Status", 
     O_ACTIVITIES.ACT_CREATED_BY as "Created by", 
     O_AGREEMENT_DETAILS.ADE_ID as "Agreement ID", 
     initcap(olm_bo.get_sty_name(O_AGREEMENT_DETAILS.ADE_STY_ID)) as "Service Type", 
     initcap(olm_bo.get_sty_name(O_SERVICE_ELEMENTS.SEL_STY_CHILD_ID)) as "Service Element", 
     O_AGREEMENT_DETAILS.ADE_START_DATE as "Agreement Start", 
     O_AGREEMENT_DETAILS.ADE_END_DATE as "Agreement End", 
     O_AGREEMENT_DETAILS.ADE_ENTERED_BY as "Entered by" 
FROM O_ACTIVITIES 
LEFT JOIN O_ACTIVITY_TYPES 
     ON O_ACTIVITY_TYPES.ACT_CLASS= O_ACTIVITIES.ACT_CLASS and 
      O_ACTIVITY_TYPES.ACT_TYPE=O_ACTIVITIES.ACT_TYPE AND 
      initcap(O_ACTIVITY_TYPES.ACT_DESC) = 'X' 
LEFT OUTER JOIN O_AGREEMENT_DETAILS 
     ON O_AGREEMENT_DETAILS.ADE_SUBJECT_ID=O_ACTIVITIES.ACT_SUBJECT_ID AND 
      initcap(olm_bo.get_sty_name(O_AGREEMENT_DETAILS.ADE_STY_ID)) IN ('Budgets') AND 
      O_AGREEMENT_DETAILS.ADE_START_DATE >= O_ACTIVITIES.ACT_REQUESTED_DATE 
LEFT JOIN O_SERVICE_ELEMENTS 
     ON O_AGREEMENT_DETAILS.ADE_SEL_ID=O_SERVICE_ELEMENTS.SEL_ID AND 
      initcap(olm_bo.get_sty_name(O_SERVICE_ELEMENTS.SEL_STY_CHILD_ID)) IN ('Payment') 
WHERE (O_ACTIVITIES.ACT_SYSTEM_IND IS NULL and 
     NVL(O_ACTIVITIES.ACT_REC_TYPE,'???') NOT IN ('Y') ) AND 
     initcap(O_ACTIVITY_TYPES.ACT_DESC) = 'X' 

的問題是,它包含了協議類型描述中的服務元素表是從協議表獨立。我可以使用協議類型代碼(實際上將協議類型加入到協議表中),但是,我真的只想知道是否可以實現協議表和協議類型表之間的內部聯接,而不影響活動表。

任何指針和建議的讚賞,希望我已經充分解釋自己。

回答

1

嘗試:

SELECT O_ACTIVITIES.ACT_SUBJECT_ID as "ID", 
     initcap(olm_bo.get_per_name(O_ACTIVITIES.ACT_SUBJECT_ID)) as "Name", 
     O_ACTIVITIES.ACT_ID as "Activity ID", 
     initcap(O_ACTIVITY_TYPES.ACT_DESC) as "Activity Type", 
     O_ACTIVITIES.ACT_REQUESTED_DATE as "Start Date", 
     case when olm_bo.get_ref_desc(O_ACTIVITIES.ACT_STATUS,'ACTIVITY_STATUS') = 'Newly generated' 
        then null 
        else O_ACTIVITIES.ACT_STATUS_DATE 
      end as "End Date", 
     olm_bo.get_ref_desc(O_ACTIVITIES.ACT_STATUS,'ACTIVITY_STATUS') as "Status", 
     O_ACTIVITIES.ACT_CREATED_BY as "Created by", 
     O_AGREEMENT_DETAILS.ADE_ID as "Agreement ID", 
     initcap(olm_bo.get_sty_name(O_AGREEMENT_DETAILS.ADE_STY_ID)) as "Service Type", 
     initcap(olm_bo.get_sty_name(O_SERVICE_ELEMENTS.SEL_STY_CHILD_ID)) as "Service Element", 
     O_AGREEMENT_DETAILS.ADE_START_DATE as "Agreement Start", 
     O_AGREEMENT_DETAILS.ADE_END_DATE as "Agreement End", 
     O_AGREEMENT_DETAILS.ADE_ENTERED_BY as "Entered by" 
FROM O_ACTIVITIES 
LEFT JOIN O_ACTIVITY_TYPES 
     ON O_ACTIVITY_TYPES.ACT_CLASS= O_ACTIVITIES.ACT_CLASS and 
      O_ACTIVITY_TYPES.ACT_TYPE=O_ACTIVITIES.ACT_TYPE AND 
      initcap(O_ACTIVITY_TYPES.ACT_DESC) = 'X' 
LEFT JOIN O_AGREEMENT_DETAILS 
      JOIN O_SERVICE_ELEMENTS 
      ON O_AGREEMENT_DETAILS.ADE_SEL_ID=O_SERVICE_ELEMENTS.SEL_ID AND 
       initcap(olm_bo.get_sty_name(O_SERVICE_ELEMENTS.SEL_STY_CHILD_ID)) IN ('Payment') 
     ON O_AGREEMENT_DETAILS.ADE_SUBJECT_ID=O_ACTIVITIES.ACT_SUBJECT_ID AND 
      initcap(olm_bo.get_sty_name(O_AGREEMENT_DETAILS.ADE_STY_ID)) IN ('Budgets') AND 
      O_AGREEMENT_DETAILS.ADE_START_DATE >= O_ACTIVITIES.ACT_REQUESTED_DATE 
WHERE (O_ACTIVITIES.ACT_SYSTEM_IND IS NULL and 
     NVL(O_ACTIVITIES.ACT_REC_TYPE,'???') NOT IN ('Y') ) AND 
     initcap(O_ACTIVITY_TYPES.ACT_DESC) = 'X' 
+0

再一次,這正是我想要的。我發現通過在協議詳細信息左連接中的'on'之前插入服務元素表,可以將兩個左連接組合在一起。我甚至從來沒有想過這樣做,所以謝謝。 – bawpie 2012-03-15 12:36:10

1

最簡單的方法(因爲它是一個怪物SQL)是包裝它並使用case語句例如

SELECT ID, Activity, Case When Agreement_Type IS NULL THEN NULL ELSE Agreement END, Agreement_Type 
FROM 
(All the original SQL goes in here) 

所以基本上全程運行你的第一個SQL的,因爲它是不是通過挑選自己的方式更輕鬆,順便說一句,它總是以避免在列名的空間是一個好主意圍繞另一個SQL所以我用在_這個案例。

+0

謝謝,我也認爲這些方針的東西,但是,我認爲這將是更好的做法,實際上是從回來擺在首位(見下面的MB的解決方案)阻止他們。 – bawpie 2012-03-15 12:39:04

+0

@bawpie公平點,它也是值得別名表名的時候,因爲這應該會顯着減少你的SQL的大小,所以oracle連接也更容易使用(+)而不是「左」。 – 2012-03-15 13:51:34