1

我正在从事 Teradata 到 Oracle 的迁移项目。如何修改以下在 Teradata 中使用 QUALIFY 的查询。

//查询1

   SELECT S.ID  as Id,
          S.MP_CD as Code,
          S.GM_CD  as GmCode,
          S.GM_MSR_NBR as Mea_Year, 
          S.STTS_CD as YearCode,
          S.TRMNTN_DTM as TerminationDate 
     FROM PD.RVY S, LOAD_LOG TLL 
    WHERE S.UPDTD_LOAD = TLL.LOG_KEY AND TLL.BLSH_CD = 'Y' AND S.STTS_CD IN ( 'C', 'P' ) 
  QUALIFY ROW_NUMBER () OVER (PARTITION BY S.GM_CD ,S.MP_CD ,S.GM_MSR_NBR,S.STTS_CD 
                              ORDER BY S.SO_DTM DESC 
                              ) = 1;

//查询2

  SELECT SP.ID,
         SP.SO_DTM,
         SP.TAX_ID,
         SP.USER_ID,
         SP.FRST_NM,
         SP.LAST_NM,
         SP.PHONE_NBR,
         QSRP.TAX_ID,
         QSRP.ROW_ID,
         MAX(SP.SO_DTM) OVER (PARTITION BY SP.ID, SP.TAX_ID) MAX_SO_DTM       
    FROM VOPR_RMSY SP,VOPR_RMSY_SPNS QSRP
    WHERE  SP.ID =:URVYID AND QSRP.TAX_ID =:RPAXID 
          AND SP.ID = QSRP.ID AND SP.TAX_ID = QSRP.TAX_ID AND SP.SO_DTM = QSRP.SO_DTM 
    QUALIFY (SP.SO_DTM=MAX_SO_DTM AND QSRP.SO_DTM = MAX_SO_DTM) 
    GROUP BY SP.ID,SP.SO_DTM,SP.TAX_ID,SP.USER_ID,SP.FRST_NM,SP.LAST_NM,SP.PHONE_NBR,
            QSRP.TAX_ID,QSRP.ROW_ID;

为此,尝试使用 HAVING 而不是限定,但出现错误:

ORA-00904: "MAX_SO_DTM": 无效标识符 00904. 00000 - "%s: 无效标识符"

似乎用于 MAX 的别名在这里不起作用....

非常感谢您的任何帮助!

编辑:

SELECT * FROM
  (         
 SP.ID,
     SP.SO_DTM,
     SP.TAX_ID,
     SP.USER_ID,
     SP.FRST_NM,
     SP.LAST_NM,
     SP.PHONE_NBR,
     QSRP.TAX_ID,
     QSRP.ROW_ID,
     MAX(SP.SO_DTM) OVER (PARTITION BY SP.ID, SP.TAX_ID) AS MAX_SO_DTM       
FROM VOPR_RMSY SP,VOPR_RMSY_SPNS QSRP 
WHERE  SP.ID =:URVYID AND QSRP.TAX_ID =:RPAXID AND SP.ID = QSRP.ID AND SP.TAX_ID =  
       QSRP.TAX_ID AND SP.SO_DTM = QSRP.SO_DTM 
GROUP BY SP.ID,SP.SO_DTM,SP.TAX_ID,SP.USER_ID,SP.FRST_NM,SP.LAST_NM,SP.PHONE_NBR,
        QSRP.TAX_ID,QSRP.ROW_ID;
 )dt WHERE (SP.SO_DTM=MAX_SO_DTM AND QSRP.SO_DTM = MAX_SO_DTM) 

我知道我必须对外部 WHERE 使用别名 dt 而不是 SP 和 QSRP,但是这里将 MAX_SO_DTM 与来自两个不同表的 SO_DTM 进行比较。还有其他方法可以修改吗?

谢谢!

4

1 回答 1

5

QUALIFY 和重用别名都是 Teradata 特定的。

您对 HAVING 的尝试失败了,因为这是处理查询的逻辑顺序:

FROM
WHERE
GROUP BY
HAVING
OLAP-function
QUALIFY -- Teradata specific
SAMPLE or EXPAND ON  -- both are Teradata specific
ORDER

要解决此问题,您必须使用派生表/内联视图并将 QUALIFY 条件移动到外部 WHERE。

SELECT *
FROM
 (
   SELECT S.ID  as Id,
          S.MP_CD as Code,
          S.GM_CD  as GmCode,
          S.GM_MSR_NBR as Mea_Year, 
          S.STTS_CD as YearCode,
          S.TRMNTN_DTM as TerminationDate,
          ROW_NUMBER () OVER (PARTITION BY S.GM_CD ,S.MP_CD ,S.GM_MSR_NBR,S.STTS_CD 
                          ORDER BY S.SO_DTM DESC) AS rn 
     FROM PD.RVY S, LOAD_LOG TLL 
    WHERE S.UPDTD_LOAD = TLL.LOG_KEY AND TLL.BLSH_CD = 'Y' AND S.STTS_CD IN ( 'C', 'P' ) 
  ) dt
WHERE rn = 1;

当您想要重用别名时,必须使用相同的技术。

编辑:第二个查询可以重写为:

SELECT * 
FROM
 (        
   SELECT 
        SP.ID,
        SP.SO_DTM,
        SP.TAX_ID,
        SP.USER_ID,
        SP.FRST_NM,
        SP.LAST_NM,
        SP.PHONE_NBR,
        QSRP.TAX_ID,
        QSRP.ROW_ID,
        MAX(SP.SO_DTM) OVER (PARTITION BY SP.ID, SP.TAX_ID) AS MAX_SO_DTM       
   FROM VOPR_RMSY SP,VOPR_RMSY_SPNS QSRP 
   WHERE SP.ID =:URVYID 
     AND QSRP.TAX_ID =:RPAXID 
     AND SP.ID = QSRP.ID 
     AND SP.TAX_ID =  QSRP.TAX_ID 
     AND SP.SO_DTM = QSRP.SO_DTM
   GROUP BY SP.ID,SP.SO_DTM,SP.TAX_ID,SP.USER_ID,SP.FRST_NM,SP.LAST_NM,SP.PHONE_NBR,
           QSRP.TAX_ID,QSRP.ROW_ID
 )dt
WHERE SO_DTM=MAX_SO_DTM 

您不需要同时进行两个比较 *(SP.SO_DTM=MAX_SO_DTM AND QSRP.SO_DTM = MAX_SO_DTM)*,因为它们第二个是多余的,因为表是在 *SP.SO_DTM = QSRP.SO_DTM* 上连接的。

否则,您必须使用不同的别名(例如 QSRP_SO_DTM)将 QSRP.SO_DTM 添加到派生表中。在外层没有更多的 SP/QSRP 而只有 dt,所以它将是:

WHERE (SO_DTM=MAX_SO_DTM AND QSRP_SO_DTM = MAX_SO_DTM)
于 2013-09-16T06:10:03.003 回答