Amazon Redshift
数据库开发人员指南 (API Version 2012-12-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

NTILE 开窗函数示例

以下示例将于 2008 年 8 月 26 日购买 Hamlet 门票所付价格划分到四个排名组中。结果集为 17 个行,几乎均匀地划分到排名 1 到 4 中:

Copy
select eventname, caldate, pricepaid, ntile(4) over(order by pricepaid desc) from sales, event, date where sales.eventid=event.eventid and event.dateid=date.dateid and eventname='Hamlet' and caldate='2008-08-26' order by 4; eventname | caldate | pricepaid | ntile -----------+------------+-----------+------- Hamlet | 2008-08-26 | 1883.00 | 1 Hamlet | 2008-08-26 | 1065.00 | 1 Hamlet | 2008-08-26 | 589.00 | 1 Hamlet | 2008-08-26 | 530.00 | 1 Hamlet | 2008-08-26 | 472.00 | 1 Hamlet | 2008-08-26 | 460.00 | 2 Hamlet | 2008-08-26 | 355.00 | 2 Hamlet | 2008-08-26 | 334.00 | 2 Hamlet | 2008-08-26 | 296.00 | 2 Hamlet | 2008-08-26 | 230.00 | 3 Hamlet | 2008-08-26 | 216.00 | 3 Hamlet | 2008-08-26 | 212.00 | 3 Hamlet | 2008-08-26 | 106.00 | 3 Hamlet | 2008-08-26 | 100.00 | 4 Hamlet | 2008-08-26 | 94.00 | 4 Hamlet | 2008-08-26 | 53.00 | 4 Hamlet | 2008-08-26 | 25.00 | 4 (17 rows)