Example: Remaining Sum - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

The following query returns the remaining sum of meat sales for all cities. There is no PARTITION BY clause in the SUM function, so all cities are included in the remaining sum.

SELECT city, kind, sales,
SUM(sales) OVER (ORDER BY city, kind
                 ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
FROM monthly;

The possible results of the preceding SELECT appear in the following table.

city     kind           sales    Remaining Sum(sales)
-------  -------------  -------  --------------------
Omaha    variety pack   25       ?
Omaha    pure pork      125      25
Omaha    pure pork      25       150
Omaha    pure pork      45       175
Chicago  variety pack   55       220
Chicago  variety pack   25       275
Chicago  variety pack   45       300
Chicago  pure pork      50       345

The sort order for the computation is alphabetical by city, and then by kind. The results, however, appear in the reverse order.

The sort order that you specify in the window specification defines the sort order of the rows over which the function is applied, not the ordering of the results. To order the results, use an ORDER BY phrase in the SELECT statement.

For example:

SELECT city, kind, sales,
SUM(sales) OVER (ORDER BY city, kind
                 ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
FROM monthly
ORDER BY city, kind;

The possible results of the preceding SELECT appear in the following table:

city     kind           sales    Remaining Sum(sales)
-------  -------------  -------  --------------------
Chicago  pure pork      50       345
Chicago  variety pack   55       265
Chicago  variety pack   25       320
Chicago  variety pack   45       220
Omaha    pure pork      25       70
Omaha    pure pork      125      95
Omaha    pure pork      45       25
Omaha    variety pack   25       ?
Computation Function
Cumulative sum
  • SUM window function
  • CSUM
Cumulative, group, or moving count COUNT window function
Group sum SUM window function
Moving average
  • AVG window function
  • MAVG
Moving difference between the current row-column value and the preceding n th row-column value MDIFF
Moving linear regression MLINREG
Moving sum
  • SUM window function
  • MSUM
Quantile scores for the values in a column QUANTILE
Ordered rank of all rows in a group
  • RANK window function
  • RANK
Relative rank of a row in a group PERCENT_RANK window function
Sequential row number of the row within its window partition according to the window ordering of the window ROW_NUMBER
Cumulative, group, or moving maximum value MAX window function
Cumulative, group, or moving minimum value MIN window function