Amazon Redshift
数据库开发人员指南 (API Version 2012-12-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

COUNT 开窗函数示例

从数据窗口的开头显示销售 ID、数量和所有行的计数:

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)

有关 WINSALES 表的说明,请参阅开窗函数示例

从数据窗口的开头显示销售 ID、数量和非 null 行的计数。(在 WINSALES 表中,QTY_SHIPPED 列包含一些 NULL 值。)

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)