例 - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL関数、式、および述部

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
17.00
Published
2020年6月
ft:locale
ja-JP
ft:lastEdition
2021-03-30
dita:mapPath
ja-JP/tpt1555966086716.ditamap
dita:ditavalPath
ja-JP/tpt1555966086716.ditaval
dita:id
B035-1145
Product Category
Software
Teradata Vantage

次の例では、残高増加の連続性を検索します。そのため、現在の残高が直前(前月末)の残高以下になるたびにリセットを実行する必要があります。

   SELECT account_key, month, balance,
   ROW_NUMBER() over
        (PARTITION BY account_key
         ORDER BY month
         RESET WHEN balance /* current row balance */ <=
         SUM(balance) over (PARTITION BY account_key ORDER BY month
         ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) /* prev row */
         ) - 1 /* to get the count started at 0 */ as balance_increase
   FROM accounts;

上記のSELECT文の結果は、例えば以下のようなテーブルになります。

account_key        month      balance  balance_increase
-----------        -----      -------  ----------------
          1            1           60                 0
          1            2           99                 1
          1            3           94                 0
          1            4           90                 0
          1            5           80                 0
          1            6           88                 1
          1            7           90                 2
          1            8           92                 3
          1            9           10                 0
          1           10           60                 1
          1           11           80                 2
          1           12           10                 0