在oracle的另一个case语句中使用case语句的结果

cnjp1d6j  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(466)

我有一个视图,其中包含一列的cast语句。此cast语句包含case语句。这句话很管用。此语句的结果是1、2或3。
从这里开始,我需要使用上一个case语句的结果(我使用了一个with语句,但它不起作用)来确定列的值。一个简单的case语句,将yes、no或null赋给上述语句的值(1、2或3)
感谢您的帮助。谢谢您。
使用伪代码的示例:

CAST (
   WITH case_output 
   AS(
    SELECT
        CASE
            WHEN EXISTS
                    (select from table where blah blah)
                THEN 
                    (select column from that table)
            ELSE
                 (select from some another table)
        END 
       )     
  CASE
        WHEN case_output = 1
            THEN 'Yes'
        WHEN case_output = 2
            THEN 'No'
    else 
        NULL
 AS VARCHAR2 (10))
    column_name,
    .... [rest of query]
5jdjgkvh

5jdjgkvh1#

您将查询名称和 WITH 条款。例如,它是

WITH my_query AS (SELECT c1 AS my_column FROM t1)
SELECT my_column FROM my_query;

其次,你总是需要一个 FROM oracle的sql中的子句。使用虚拟表 DUAL 作为替身:

SELECT CASE WHEN ... THEN END AS my_column
  FROM DUAL;

最小工作示例:

CREATE TABLE t1 (c1 INT);
CREATE TABLE t2 (c2 INT);
INSERT INTO t1 VALUES (1);
INSERT INTO t2 VALUES (2);

WITH case_query AS (
   SELECT CASE WHEN EXISTS (SELECT * FROM t1 WHERE c1=100) 
               THEN (SELECT c1 FROM t1)
               ELSE (SELECT c2 FROM t2)
           END AS case_output 
     FROM dual)
SELECT CASE case_output
       WHEN 1 THEN 'Yes'
       WHEN 2 THEN 'No'
       ELSE NULL
        END second_case_output
  FROM case_query;

相关问题