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

PERCENTILE_DISC 开窗函数示例

以下示例使用 WINSALES 表。有关 WINSALES 表的说明,请参阅开窗函数示例

Copy
select sellerid, qty, percentile_disc(0.5) within group (order by qty) over() as median from winsales; sellerid | qty | median ----------+-----+-------- 1 | 10 | 20 3 | 10 | 20 1 | 10 | 20 4 | 10 | 20 3 | 15 | 20 2 | 20 | 20 2 | 20 | 20 3 | 20 | 20 1 | 30 | 20 3 | 30 | 20 4 | 40 | 20 (11 rows)
Copy
select sellerid, qty, percentile_disc(0.5) within group (order by qty) over(partition by sellerid) as median from winsales; sellerid | qty | median ----------+-----+-------- 2 | 20 | 20 2 | 20 | 20 4 | 10 | 10 4 | 40 | 10 1 | 10 | 10 1 | 10 | 10 1 | 30 | 10 3 | 10 | 15 3 | 15 | 15 3 | 20 | 15 3 | 30 | 15 (11 rows)