Keywords: Teradata | Window Functions | ROWS UNBOUNDED PRECEDING | Running Total | SQL Analytic Functions
Abstract: This paper provides an in-depth examination of the ROWS UNBOUNDED PRECEDING window function in Teradata databases. Through comparative analysis with standard SQL window framing, combined with typical scenarios such as cumulative sums and moving averages, it systematically explores the core role of unbounded preceding clauses in data accumulation calculations. The article employs progressive examples to demonstrate implementation paths from basic syntax to complex business logic, offering complete technical reference for practical window function applications.
Fundamental Concepts of Window Function Framing
In standard SQL specifications, window functions define computation scope through the OVER clause, where the ROWS BETWEEN ... AND ... syntax specifies concrete row range frames. This mechanism enables aggregate operations on specific data ranges while maintaining the original row structure.
Limited Range Window Function Example
To establish foundational understanding, first examine window computations with limited ranges. The following example demonstrates moving average calculation centered on the current row, extending one row forward and backward:
WITH sample_data (time_point, value) AS (
VALUES(1, 1),
(2, 5),
(3, 3),
(4, 5),
(5, 4),
(6, 11)
)
SELECT time_point, value,
AVG(value) OVER (ORDER BY time_point ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
FROM sample_data
ORDER BY time_point
Execution results display each time point's value and its three-row moving average:
Time Point Value Average
-------------------------
1 1 3.00
2 5 3.00
3 3 4.33
4 5 4.00
5 4 6.67
6 11 7.50
Core Characteristics of Unbounded Preceding Framing
ROWS UNBOUNDED PRECEDING extends the window's lower bound to the partition's starting position, creating cumulative computation ranges from the first row to the current row. This pattern is particularly suitable for scenarios requiring complete historical data accumulation.
Practical Demonstration of Cumulative Summation
Running Total represents a typical application of unbounded preceding framing:
WITH sample_data (sequence, amount) AS (
VALUES(1, 1),
(2, 5),
(3, 3),
(4, 5),
(5, 4),
(6, 11)
)
SELECT sequence, amount,
SUM(amount) OVER (ORDER BY sequence ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
FROM sample_data
ORDER BY sequence
Calculation results clearly present the row-by-row accumulation process:
Sequence Amount Running Total
------------------------------
1 1 1
2 5 6
3 3 9
4 5 14
5 4 18
6 11 29
Detailed Explanation of Frame Boundary Definitions
Window function frames support multiple boundary definition methods:
CURRENT ROW: Limited to the currently processed rowUNBOUNDED PRECEDING: Extended to the partition's first row (fixed boundary)UNBOUNDED FOLLOWING: Extended to the partition's last row (fixed boundary)x PRECEDING: x rows before the current row (relative boundary)y FOLLOWING: y rows after the current row (relative boundary)
Application Scenario Classification
Based on boundary combination methods, window functions primarily serve three computational requirements:
- Full Partition Aggregation: Dual fixed boundaries achieve coexistence of group statistics and detailed data
- Dynamic Range Computation: Combinations of fixed and relative boundaries support variable-length window calculations like cumulative values and remaining sums
- Fixed Window Statistics: Dual relative boundaries ensure constant window sizes for scenarios like moving averages
Technical Implementation Key Points
In Teradata environments, SUM(x) OVER (ORDER BY column ROWS UNBOUNDED PRECEDING) constructs standard running total patterns. Computation logic follows row-by-row accumulation principles: the first row's value equals itself, while subsequent rows' values equal the sum of previous cumulative totals and current values. This mechanism holds significant application value in business scenarios such as financial accumulation and inventory tracking.