CSUM Function Examples | Teradata Vantage - 17.10 - Example - Advanced SQL Engine - Teradata Database

Teradata Vantageā„¢ - SQL Functions, Expressions, and Predicates

Advanced SQL Engine
Teradata Database
Release Number
Release Date
July 2021
Content Type
Programming Reference
Publication ID
English (United States)

Report the daily running sales total for product code 10 for each month of 1998.

   SELECT cmonth, CSUM(sumPrice, cdate)
   (SELECT a2.month_of_year,
   a2.calendar_date,a1.itemID, SUM(a1.price)
   FROM Sales a1, SYS_CALENDAR.Calendar a2
   WHERE a1.calendar_date=a2.calendar_date
   AND a2.calendar_date=1998
   AND a1.itemID=10
   GROUP BY a2.month_of_year, a1.calendar_date,
   a1.itemID) AS T1(cmonth, cdate, sumPrice)
   GROUP BY cmonth;

Grouping by month allows the total to accumulate until the end of each month, when it is then set to zero for the next month. This permits the calculation of cumulative totals for each item in the same query.