文档首页 > > 工具指南> Migration Tool SQL语法迁移工具> Teradata语法迁移> 系统函数和操作符> 窗函数

窗函数

分享
更新时间: 2019/08/09 GMT+08:00

在查询结果中,窗函数会跨行执行计算。Migration Tool支持以下Teradata窗口自函数:

说明:

Migration Tool仅支持QUALIFY语句中存在一个WINDOW函数。如果QUALIFY语句中存在多个WINDOW函数,可能会导致迁移失败。

CSUM

累积和(CSUM)函数为列的数值提供运行或累计总数,建议在QUALIFY语句中使用ALIAS。

输入:CSUM和GROUP_ID
INSERT INTO GSIS_SUM.DW_DAT71 (
   col1
  ,PROD_GROUP
)
   SELECT 
      CSUM(1, T1.col1)
      ,T1.PROD_GROUP
     FROM tab1  T1
    WHERE T1.col1 = 'ABC'
;

输出

INSERT
     INTO
          GSIS_SUM.DW_DAT71 (
               col1
               ,PROD_GROUP
          ) SELECT
                    SUM (1) over( ORDER BY T1.col1 ROWS UNBOUNDED PRECEDING )
                    ,T1.PROD_GROUP
               FROM
                    tab1 T1
               WHERE
                    T1.col1 = 'ABC'
;
输入:CSUM和GROUP_ID
SELECT  top 10
      CSUM(1, T1.Test_GROUP)
      ,T1.col1
  FROM  $[schema}.  T1
 WHERE T1.Test_GROUP = 'Test_group' group by Test_group order by Test_Group;

输出

SELECT
       SUM (1) over( partition BY Test_group ORDER BY T1.Test_GROUP ROWS UNBOUNDED PRECEDING )
       ,T1.col1
  FROM
       $[schema}. T1
 WHERE
       T1.Test_GROUP = 'Test_group'
 ORDER BY
       Test_Group LIMIT 10
;
输入:CSUM、GROUP BY和QUALIFY
SELECT c1, c2, c3, CSUM(c4, c3) 
  FROM tab1 
QUALIFY ROW_NUMBER(c4) = 1  
GROUP BY 1, 2;

输出

SELECT c1, c2, c3, ColumnAlias1
  FROM ( SELECT c1, c2, c3
              , SUM (c4) OVER(PARTITION BY 1 ,2 ORDER BY c3 ROWS UNBOUNDED PRECEDING) AS ColumnAlias1
              , ROW_NUMBER( ) OVER(PARTITION BY 1, 2 ORDER BY c4) AS ROW_NUM1
           FROM tab1
       ) Q1
   WHERE Q1.ROW_NUM1 = 1;

MDIFF

MDIFF函数基于预定的查询宽度计算一列的移动差分值(Moving Difference)。查询宽度指所指定的行数。建议在QUALIFY语句中使用ALIAS。

输入:MDIFF和QUALIFY命令

SELECT DT_A.Acct_ID, DT_A.Trade_Date, DT_A.Stat_PBU_ID
      , CAST( MDIFF( Stat_PBU_ID_3, 1, DT_A.Trade_No ASC ) AS DECIMAL(20,0) ) AS MDIFF_Stat_PBU_ID
   FROM Trade_His DT_A
  WHERE Trade_Date >= CAST( '20170101' AS DATE FORMAT 'YYYYMMDD' ) 
  GROUP BY DT_A.Acct_ID, DT_A.Trade_Date
 QUALIFY MDIFF_Stat_PBU_ID <> 0 OR MDIFF_Stat_PBU_ID IS NULL;

输出

SELECT Acct_ID, Trade_Date, Stat_PBU_ID, MDIFF_Stat_PBU_ID
   FROM (SELECT DT_A.Acct_ID, DT_A.Trade_Date, DT_A.Stat_PBU_ID
         , CAST( (Stat_PBU_ID_3 - (LAG(Stat_PBU_ID_3, 1, NULL) OVER (PARTITION BY DT_A.Acct_ID, DT_A.Trade_Date ORDER BY DT_A.Trade_No ASC)))  AS MDIFF_Stat_PBU_ID
           FROM Trade_His DT_A
          WHERE Trade_Date >= CAST( '20170101' AS DATE)
                )
 WHERE MDIFF_Stat_PBU_ID <> 0 OR MDIFF_Stat_PBU_ID IS NULL;

RANK

RANK(col1, col2...)

输入:RANK和GROUP BY

SELECT  c1, c2, c3, RANK(c4, c1 DESC, c3) AS Rank1 
  FROM  tab1 
 WHERE  ... 
 GROUP BY c1;

输出

SELECT c1, c2, c3, RANK() OVER (PARTITION BY c1 ORDER BY c4, c1 DESC ,c3) AS Rank1
  FROM tab1
 WHERE ...;

ROW_NUMBER

ROW_NUMBER(col1, col2...)

输入:ROW NUMBER、GROUP BY和QUALIFY

SELECT c1, c2, c3, ROW_NUMBER(c4, c3) 
   FROM tab1 
QUALIFY RANK(c4) = 1  
  GROUP BY 1, 2;

输出

SELECT
      c1
     ,c2
     ,c3
     ,ColumnAlias1
  FROM
      (
        SELECT
                c1
               ,c2
               ,c3
               ,ROW_NUMBER( ) over( PARTITION BY 1 ,2 ORDER BY c4 ,c3 ) AS ColumnAlias1
               ,RANK (
               ) over( PARTITION BY 1 ,2 ORDER BY c4 ) AS ROW_NUM1
          FROM
              tab1
      ) Q1
 WHERE
      Q1.ROW_NUM1 = 1
;
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区