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

MIN Window Function Examples

Show the sales ID, quantity, and minimum quantity from the beginning of the data window:

Copy
select salesid, qty, min(qty) over (order by salesid rows unbounded preceding) from winsales order by salesid; salesid | qty | min ---------+-----+----- 10001 | 10 | 10 10005 | 30 | 10 10006 | 10 | 10 20001 | 20 | 10 20002 | 20 | 10 30001 | 10 | 10 30003 | 15 | 10 30004 | 20 | 10 30007 | 30 | 10 40001 | 40 | 10 40005 | 10 | 10 (11 rows)
For a description of the WINSALES table, see Window Function Examples.

Show the sales ID, quantity, and minimum quantity in a restricted frame:

Copy
select salesid, qty, min(qty) over (order by salesid rows between 2 preceding and 1 preceding) as min from winsales order by salesid; salesid | qty | min ---------+-----+----- 10001 | 10 | 10005 | 30 | 10 10006 | 10 | 10 20001 | 20 | 10 20002 | 20 | 10 30001 | 10 | 20 30003 | 15 | 10 30004 | 20 | 10 30007 | 30 | 15 40001 | 40 | 20 40005 | 10 | 30 (11 rows)