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

COUNT Window Function Examples

Show the sales ID, quantity, and count of all rows from the beginning of the data window:

Copy
select salesid, qty, count(*) over (order by salesid rows unbounded preceding) as count from winsales order by salesid; salesid | qty | count ---------+-----+----- 10001 | 10 | 1 10005 | 30 | 2 10006 | 10 | 3 20001 | 20 | 4 20002 | 20 | 5 30001 | 10 | 6 30003 | 15 | 7 30004 | 20 | 8 30007 | 30 | 9 40001 | 40 | 10 40005 | 10 | 11 (11 rows)
For a description of the WINSALES table, see Window Function Examples.

Show the sales ID, quantity, and count of non-null rows from the beginning of the data window. (In the WINSALES table, the QTY_SHIPPED column contains some NULLs.)

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