Menu
Amazon Redshift
Database Developer Guide (API Version 2012-12-01)

AVG Window Function Examples

Compute a rolling average of quantities sold by date; order the results by date ID and sales ID:

select salesid, dateid, sellerid, qty, avg(qty) over (order by dateid, salesid rows unbounded preceding) as avg from winsales order by 2,1; salesid | dateid | sellerid | qty | avg ---------+------------+----------+-----+----- 30001 | 2003-08-02 | 3 | 10 | 10 10001 | 2003-12-24 | 1 | 10 | 10 10005 | 2003-12-24 | 1 | 30 | 16 40001 | 2004-01-09 | 4 | 40 | 22 10006 | 2004-01-18 | 1 | 10 | 20 20001 | 2004-02-12 | 2 | 20 | 20 40005 | 2004-02-12 | 4 | 10 | 18 20002 | 2004-02-16 | 2 | 20 | 18 30003 | 2004-04-18 | 3 | 15 | 18 30004 | 2004-04-18 | 3 | 20 | 18 30007 | 2004-09-07 | 3 | 30 | 19 (11 rows)

For a description of the WINSALES table, see Window Function Examples.