Amazon Redshift
数据库开发人员指南 (API 版本 2012-12-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

COUNT 开窗函数示例

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

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 值。)

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)