2015-04-02 195 views
0

我在Oracle中有一個case語句;然後我需要一個子查詢在同一個查詢中使用case語句的結果。我怎樣才能做到這一點?在同一查詢中使用case語句的Oracle子查詢

下面是該查詢的板塊和子查詢:

CL.TC_DEFECT_1, 
CASE 
    WHEN CL.TC_DEFECT_1_SELECTION = 10000000 THEN 1 
    WHEN CL.TC_DEFECT_1_SELECTION = 01000000 THEN 2 
    WHEN CL.TC_DEFECT_1_SELECTION = 00100000 THEN 3 
    WHEN CL.TC_DEFECT_1_SELECTION = 00010000 THEN 4 
    WHEN CL.TC_DEFECT_1_SELECTION = 00001000 THEN 5 
    WHEN CL.TC_DEFECT_1_SELECTION = 00000100 THEN 6 
    WHEN CL.TC_DEFECT_1_SELECTION = 00000010 THEN 7 
    WHEN CL.TC_DEFECT_1_SELECTION = 00000001 THEN 8 
END AS "DEFECT_1_SORT_ORDER", 

(SELECT TC_DESCRIPTION_FR FROM TBYE60_DEFECT_CODES DC 
WHERE DC.TC_CODE_DEF = CL.TC_DEFECT_1 
AND DC.TC_SORT_ORDER = DEFECT_1_SORT_ORDER 
AND rownum=1) AS SUB_DEFECT_1_DESC, 

回答

0

我不知道甲骨文,但你可以看到橫向JOIN。 你可以做這樣的事情:

  CL.TC_DEFECT_1, 
       "DEFECT_1_SORT_ORDER", 

        (SELECT TC_DESCRIPTION_FR FROM TBYE60_DEFECT_CODES DC WHERE DC.TC_CODE_DEF = CL.TC_DEFECT_1 AND DC.TC_SORT_ORDER = CL.DEFECT_1_SORT_ORDER AND rownum=1) AS SUB_DEFECT_1_DESC 
    ... 
    FROM 
    ... 
    CROSS JOIN TABLE(
    SELECT    CASE 
        WHEN CL.TC_DEFECT_1_SELECTION = 10000000 THEN 1 
        WHEN CL.TC_DEFECT_1_SELECTION = 01000000 THEN 2 
        WHEN CL.TC_DEFECT_1_SELECTION = 00100000 THEN 3 
        WHEN CL.TC_DEFECT_1_SELECTION = 00010000 THEN 4 
        WHEN CL.TC_DEFECT_1_SELECTION = 00001000 THEN 5 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000100 THEN 6 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000010 THEN 7 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000001 THEN 8 
       END AS "DEFECT_1_SORT_ORDER" 
from dual 

    ) a 

OR

  CL.TC_DEFECT_1, 
       "DEFECT_1_SORT_ORDER", 

        (SELECT TC_DESCRIPTION_FR FROM TBYE60_DEFECT_CODES DC WHERE DC.TC_CODE_DEF = CL.TC_DEFECT_1 AND DC.TC_SORT_ORDER = CL.DEFECT_1_SORT_ORDER AND rownum=1) AS SUB_DEFECT_1_DESC 
    ... 
    FROM 
    ... 
    LATERAL (
    SELECT    CASE 
        WHEN CL.TC_DEFECT_1_SELECTION = 10000000 THEN 1 
        WHEN CL.TC_DEFECT_1_SELECTION = 01000000 THEN 2 
        WHEN CL.TC_DEFECT_1_SELECTION = 00100000 THEN 3 
        WHEN CL.TC_DEFECT_1_SELECTION = 00010000 THEN 4 
        WHEN CL.TC_DEFECT_1_SELECTION = 00001000 THEN 5 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000100 THEN 6 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000010 THEN 7 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000001 THEN 8 
       END AS "DEFECT_1_SORT_ORDER" 
from dual 

    ) a 
+0

當我嘗試CROSS JOIN TABLE示例時,我得到一個缺少的關鍵字錯誤。 – 2015-04-02 13:36:01

+0

對不起,我沒有在哪裏檢查Oracle語法。我可以指出。 – 2015-04-02 13:40:51

+0

好的不用擔心謝謝你的信息 – 2015-04-02 13:43:19

0

因爲我看你可以很容易地使用,而不是這種情況下,聲明INSTR功能我不正是你想要的理解只是先來看看:

CASE 
    WHEN CL.TC_DEFECT_1_SELECTION = 10000000 THEN 1 
    WHEN CL.TC_DEFECT_1_SELECTION = 01000000 THEN 2 
    WHEN CL.TC_DEFECT_1_SELECTION = 00100000 THEN 3 
    WHEN CL.TC_DEFECT_1_SELECTION = 00010000 THEN 4 
    WHEN CL.TC_DEFECT_1_SELECTION = 00001000 THEN 5 
    WHEN CL.TC_DEFECT_1_SELECTION = 00000100 THEN 6 
    WHEN CL.TC_DEFECT_1_SELECTION = 00000010 THEN 7 
    WHEN CL.TC_DEFECT_1_SELECTION = 00000001 THEN 8 
END AS "DEFECT_1_SORT_ORDER" 

可以模擬爲:

INSTR(CL.TC_DEFECT_1_SELECTION,'1') as AS "DEFECT_1_SORT_ORDER" 
--you need also to check if CL.TC_DEFECT_1_SELECTION in (above collection) 

希望這會有用。

0

只需使用子查詢。在子查詢定義DEFECT_1_SORT_ORDER,然後用它在外部查詢:

select . . ., DEFECT_1_SORT_ORDER, 
     (SELECT TC_DESCRIPTION_FR 
     FROM TBYE60_DEFECT_CODES DC 
     WHERE DC.TC_CODE_DEF = CL.TC_DEFECT_1 AND 
       DC.TC_SORT_ORDER = CL.DEFECT_1_SORT_ORDER AND rownum=1 
     ) AS SUB_DEFECT_1_DESC 
from (select . . ., 
      (CASE 
        WHEN CL.TC_DEFECT_1_SELECTION = 10000000 THEN 1 
        WHEN CL.TC_DEFECT_1_SELECTION = 01000000 THEN 2 
        WHEN CL.TC_DEFECT_1_SELECTION = 00100000 THEN 3 
        WHEN CL.TC_DEFECT_1_SELECTION = 00010000 THEN 4 
        WHEN CL.TC_DEFECT_1_SELECTION = 00001000 THEN 5 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000100 THEN 6 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000010 THEN 7 
        WHEN CL.TC_DEFECT_1_SELECTION = 00000001 THEN 8 
       END) AS "DEFECT_1_SORT_ORDER" 
     from . . . 
0

你可以使用clausole WITH, 在那裏你可以有你的SUB_DEFECT_1_DESC, 然後用在那裏你會使用SUB_DEFECT_1_DESC的結果爲例。