23.6. Cumulative Statistics

A cumulative or running statistic looks at each data value and how it is related to the whole data set. The most common examples involve changes in an aggregate value over time or on some other well-ordered dimension. A bank balance, which changes with each deposit or withdrawal, is a running total over time. The total weight of a delivery truck as we add each package is a running total over the set of packages. But since two packages can have the same weight, we need a way to break ties—for example, use the arrival dates of the packages, and if that fails, use the alphabetical order of the last names of the shippers. In SQL, this means that we need a table with a key that we can use to order the rows.

Computer people ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.