Oracle中的Qualify子句

时间:2013-09-16 03:42:54

标签: oracle11g teradata

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

// QUERY 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) 

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

谢谢!

1 个答案:

答案 0 :(得分:4)

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)*,因为它们是第2个冗余的,因为表连接在* 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)