对一个DIM KEY使用两个查找功能

时间:2016-12-02 21:56:32

标签: function plsql lookup

我为数据中可能出现的三种情况创建了三种查找函数。这些函数中的每一个都应该返回一个account_dim_key,第一个方案采用sr帐户ID并从调用表中调用数据时间,如果调用日期时间在有效开始日期和结束日期之间,则返回昏暗键,第二个方案使用调用日期(没有时间)和第三个scnario只使用帐户ID(没有日期)。如果第一个查找函数返回-1,那么我想使用第二个查找函数,如果返回-1,那么我将使用第三个查找函数。我一直试图在case语句中使用它们,但在我尝试过的所有场景中,它总是从第二个查找函数返回dim键(在我的case语句的THEN部分)。是不可能以这种方式在case语句中使用三个函数,或者我是以完全错误的方式来处理它。

查找1

CREATE OR REPLACE FUNCTION LOOKUP_D_ACCT(p_acct_id   IN VARCHAR2, p_date IN DATE)
   RETURN NUMBER
   PARALLEL_ENABLE
IS
   v_dim_id   NUMBER;
BEGIN
   SELECT ACCOUNT_DIM_KEY 
     INTO v_dim_id
     FROM 
          schema.D_ACCOUNT ac
    WHERE     p_acct_id = ac.ID
              AND (p_date BETWEEN ac.EFFECTIVE_START_DT AND ac.EFFECTIVE_END_DT)
          ;

   RETURN v_dim_id;
EXCEPTION
   WHEN OTHERS
   THEN
      RETURN -1;
END LOOKUP_D_ACCT;
/

查找2

CREATE OR REPLACE FUNCTION LOOKUP_D_ACCT_TRUNC_DT (p_acct_id   IN VARCHAR2, p_date IN DATE)
   RETURN NUMBER
   PARALLEL_ENABLE
IS
   v_dim_id   NUMBER;
BEGIN
   SELECT MAX(ACCOUNT_DIM_KEY)
     INTO v_dim_id
     FROM 
          schema.D_ACCOUNT ac
    WHERE     p_acct_id = ac.ID
                AND p_date >= TRUNC(ac.EFFECTIVE_START_DT) AND p_date <= TRUNC(ac.EFFECTIVE_END_DT)
          ;

   RETURN v_dim_id;
EXCEPTION
   WHEN OTHERS
   THEN
      RETURN -1;
END;
/

查找3

CREATE OR REPLACE FUNCTION LOOKUP_D_ACCT_NO_DT (p_acct_id   IN VARCHAR2)
   --LOOKUP FUNCTION TO PASS THROUGH FIELD_ALIGN_DIM_ID WITH DATE CRITERIA
   --new one for practice load
   --dated 8/31
   RETURN NUMBER
   PARALLEL_ENABLE
IS
   v_dim_id   NUMBER;
BEGIN
   SELECT ACCOUNT_DIM_KEY
     INTO v_dim_id
     FROM 
          schema.D_ACCOUNT ac
    WHERE     p_acct_id = ac.ID
          ;

   RETURN v_dim_id;
EXCEPTION
   WHEN OTHERS
   THEN
      RETURN -1;
END;
/

我的程序选择如下:

SELECT cl.id,
       cl.account_vod__c,
       cl.call_date_vod__c,
       cl.call_datetime_vod__c,
       (CASE
           WHEN NVL (
                   LOOKUP_D_ACCT(cl.ACCOUNT_VOD__C,
                                        CL.CALL_DATETIME_VOD__C),
                   -1) = -1
           --lookup 1
           THEN
              NVL (
                 LOOKUP_ACCT_TRUNC_DT (cl.ACCOUNT_VOD__C,
                                          CL.CALL_DATE_VOD__C),
                 -1)
           --lookup 2
           WHEN NVL (
                   LOOKUP_ACCT_TRUNC_DT (cl.ACCOUNT_VOD__C,
                                            CL.CALL_DATE_VOD__C),
                   -1) = -1
           THEN
              NVL (LOOKUP_D_ACCT_NO_DT (cl.ACCOUNT_VOD__C), -1)
           --lookup 3
           ELSE -3
        END)
          AS ACCOUNT_DIM_KEY,
       LOOKUP_D_ACCT_VEEVA (cl.ACCOUNT_VOD__C, CL.CALL_DATETIME_VOD__C)
          AS DTIME_DIM_KEY,
       --lookup 1
       LOOKUP_ACCOUNT_DIM_TEST (cl.ACCOUNT_VOD__C, CL.CALL_DATE_VOD__C)
          AS DT_DIM_KEY,
       --lookup 2
       LOOKUP_D_ACCT_VEEVA_NO_DT (cl.ACCOUNT_VOD__C) AS NODT_DIM_KEY
  --lookup 3
  FROM schema.CALLTABLE CL
 WHERE CL.ID IN ('a043600000Bija3AAB')

我希望'ACCOUNT_DIM_KEY'字段始终使用查找1,2或3中的函数填充 enter image description here

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

如果你想传递条件的所有链条,那么你不应该使用if (NVL (LOOKUP_D_ACCT(cl.ACCOUNT_VOD__C, CL.CALL_DATETIME_VOD__C), -1) = -1) then if (NVL ( LOOKUP_ACCT_TRUNC_DT (cl.ACCOUNT_VOD__C, CL.CALL_DATE_VOD__C), -1) = -1 ) then if (NVL (LOOKUP_D_ACCT_NO_DT (cl.ACCOUNT_VOD__C), -1) = -1) then -- if third func returned -1 else -- action if third case end if; else --action if second case end if; else --action if first case end if; 语句,而是连续检查每个条件:

case

<强> UPD 当然,如果它是 SQL 而不是PL / SQL,那么你应该使用嵌套的SELECT (CASE WHEN NVL ( LOOKUP_D_ACCT(cl.ACCOUNT_VOD__C, CL.CALL_DATETIME_VOD__C), -1) = -1 --lookup 1 THEN CASE WHEN NVL ( LOOKUP_ACCT_TRUNC_DT (cl.ACCOUNT_VOD__C, CL.CALL_DATE_VOD__C), -1)= -1 THEN NVL (LOOKUP_D_ACCT_NO_DT (cl.ACCOUNT_VOD__C), -1) ELSE -3 END WHEN NVL ( LOOKUP_ACCT_TRUNC_DT (cl.ACCOUNT_VOD__C, CL.CALL_DATE_VOD__C), -1) = -1 THEN NVL (LOOKUP_D_ACCT_NO_DT (cl.ACCOUNT_VOD__C), -1) --lookup 3 ELSE -3 END) AS ACCOUNT_DIM_KEY, LOOKUP_D_ACCT_VEEVA (cl.ACCOUNT_VOD__C, CL.CALL_DATETIME_VOD__C) AS DTIME_DIM_KEY, --lookup 1 LOOKUP_ACCOUNT_DIM_TEST (cl.ACCOUNT_VOD__C, CL.CALL_DATE_VOD__C) AS DT_DIM_KEY, --lookup 2 LOOKUP_D_ACCT_VEEVA_NO_DT (cl.ACCOUNT_VOD__C) AS NODT_DIM_KEY --lookup 3 FROM schema.CALLTABLE CL WHERE CL.ID IN ('a043600000Bija3AAB') ,如上所述:

numberOfFiles < MAX_FILE_VALUE