oracle查询嵌套选择用例

时间:2012-04-17 19:08:33

标签: sql oracle

我有(2)案例陈述:

SELECT CASE WHEN EXISTS
                   ( SELECT *
                     FROM MYTABLE_A
                     WHERE timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
                   )
            THEN 0
            ELSE 1
       END AS FLAG_MYTABLE_A
from DUAL;
SELECT CASE WHEN EXISTS
                   ( SELECT *, timestamp
                     FROM MYTABLE_B
                     WHERE
                     timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
                   )                       )
            THEN 0
            ELSE 1
       END AS FLAG_MYTABLE_B
from DUAL;

结果:

FLAG_MYTABLE_A
--------------
             0

FLAG_MYTABLE_B
--------------
         1

我需要帮助构建此查询,以便显示以下结果。

MYTABLENAME       MYFLAG
-----------       ------
MYTABLE_A              0
MYTABLE_B              1

我在看嵌套选择吗?有人可以告诉我如何设置它吗?

2 个答案:

答案 0 :(得分:5)

您可以使用UNION ALL

SELECT 'MYTABLE_A' AS MYTABLENAME, CASE WHEN EXISTS
                   ( SELECT *
                     FROM MYTABLE_A
                     WHERE timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
                   )
            THEN 0
            ELSE 1
       END AS FLAG_MYTABLE_A
from DUAL
UNION ALL
SELECT 'MYTABLE_B' AS MYTABLENAME, CASE WHEN EXISTS
                   ( SELECT *, timestamp
                     FROM MYTABLE_B
                     WHERE
                     timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
                   )                       )
            THEN 0
            ELSE 1
       END AS FLAG_MYTABLE_B
from DUAL;

答案 1 :(得分:1)

您可以执行以下操作:

SELECT "MYTABLE_A", CASE ... END AS "MYFLAG" FROM DUAL
UNION
SELECT "MYTABLE_B", CASE ... END AS "MYFLAG" FROM DUAL