2013-11-21 51 views
-2
Select 
Xyz, 
CASE 
    WHEN 'AEJ-DODGE-AC-' 
     ||measure IN 
     (SELECT child 
     FROM interim_dim_measures_mas 
     WHERE child LIKE 'AEJ-DODGE-AC%' 
     CONNECT BY prior child = parent 
     START WITH parent  = 'Total Revenue (GBS).1' 
    ) 
    THEN -1*CAST(datavalue AS FLOAT) 
    ELSE CAST(datavalue AS FLOAT) 
    END AS amount 
FROM interim_data_misbaej, 
    w_csrbrd 
WHERE LENGTH(period) = 3 
AND 'AEJ-BK-' 
    || book = region_book(+) 
+0

這是一個非常不尋常的語法。你想用||完成什麼?施工?另外,爲什麼它有一個問題,有一個子查詢?最後,您現有的查詢是否真的可以正常工作? – catfood

+0

那'''部分不重要;子查詢執行5億次的問題並且導致性能下降;表interim_data_misbaej有450萬行,w_csrbrd有5000行;所以想知道從select子句中刪除子查詢的等效sql – user3018037

+0

好吧,我發現這是一個Oracle問題,我沒有資格非常有幫助。但是我確實修復了標籤,所以也許別人會選擇它。 – catfood

回答

1

可以使用LEFT JOIN迫使這只是一次運行,GROUP BY和COUNT,以確定是否匹配已經發現:

Select Xyz, 
     CASE 
     WHEN COUNT(m.child) THEN -1*CAST(datavalue AS FLOAT) 
     ELSE CAST(datavalue AS FLOAT) 
     END AS amount 
FROM interim_data_misbaej, w_csrbrd 
LEFT JOIN (
    SELECT child 
    FROM interim_dim_measures_mas 
    WHERE child LIKE 'AEJ-DODGE-AC%' 
    CONNECT BY prior child = parent 
    START WITH parent  = 'Total Revenue (GBS).1' 
) m ON ('AEJ-DODGE-AC-' || measure) = m.child 
WHERE LENGTH(period) = 3 
    AND 'AEJ-BK-' || book = region_book(+) 
GROUP BY Xyz, measure, datavalue