| 1 | | | | | | ral | | | | | | | | web- | | | | | | | | nabl | | | | | | | | d | | | | | | | | defi | | | | | | | | itio | | | | | | | | | | | | | +----+---------------+------+--------+---------+-------------+-------------+ | 10 | Rhyloo | Publ | Chris | 965477. | 2016-11-07 | Prospecting | | | | c-ke | | 4 | | | | | | | | | | | | | | cohe | | | | | | | | ent | | | | | | | | infr | | | | | | | | stru | | | | | | | | ture | | | | | +----+---------------+------+--------+---------+-------------+-------------+ The schema, sample data, and queries are available as an attachment to this article. Cumulative Sum and Running Max Example -------------------------------------- Window functions can be used to achieve cumulative / running calculations on a detail report. In this case a won opportunity report for a 7 day period adds columns to show the accumulated won amount as well as the current highest opportunity amount in preceding rows. select owner, accountName, CloseDate, amount, sum(amount) over (order by CloseDate rows between unbounded preceding and current row) cumeWon, max(amount) over (order by CloseDate rows between unbounded preceding and current row) runningMax from opportunities where stageName='ClosedWon' and closeDate >= '2016-10-02' and closeDate <= '2016-10-09' order by CloseDate; with example results: +--------+---------------+-------------+---------+----------+--------------+ | owner | accountName | CloseDate | amount | cumeWon | runningMax | +--------+---------------+-------------+---------+----------+--------------+ | Bill | Babbleopia | 2016-10-02 | 437636. | 437636.4 | 437636.47 | | | | | 7 | | | +--------+---------------+-------------+---------+----------+--------------+ | Bill | Thoughtworks | 2016-10-04 | 146086. | 583722.9 | 437636.47 | | | | | 1 | | | +--------+---------------+-------------+---------+----------+--------------+ | Olivie | Devpulse | 2016-10-05 | 834235. | 1417958. | 834235.93 | | | | | 3 | 1 | | +--------+---------------+-------------+---------+----------+--------------+ | Chris | Linkbridge | 2016-10-07 | 539977. | 2458738. | 834235.93 | | | | | 5 | 5 | | +--------+---------------+-------------+---------+----------+--------------+ | Olivie | Trupe | 2016-10-07 | 500802. | 1918761. | 834235.93 | | | | | 9 | 0 | | +--------+---------------+-------------+---------+----------+--------------+ | Bill | Latz | 2016-10-08 | 857254. | 3315993. | 857254.87 | | | | | 7 | 2 | | +--------+---------------+-------------+---------+----------+--------------+ | Chris | Avamm | 2016-10-09 | 699566. | 4015560. | 857254.87 | | | | | 6 | 8 | | +--------+---------------+-------------+---------+----------+--------------+ Partitioned Cumulative Sum and Running Max Example -----