Oracle子查询在同一查询中使用case语句

时间:2015-04-02 13:10:52

标签: sql oracle11g

我在Oracle中有一个案例陈述;然后,我需要一个子查询来在同一个查询中使用case语句的结果。我怎么能这样做?

以下是查询和子查询的部分:

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,

4 个答案:

答案 0 :(得分:0)

我不知道oracle,但你可以阅读有关LATERAL 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

答案 1 :(得分:0)

我一眼就看不出你想要的东西,因为我看到你可以轻松使用INSTR函数代替那个case语句:

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) 

希望这很有用。

答案 2 :(得分: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 . . .

答案 3 :(得分:0)

你可以使用clausole WITH, 你可以在哪里获得SUB_DEFECT_1_DESC, 然后使用CASE,您将使用SUB_DEFECT_1_DESC的结果。