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

PERCENTILE_DISC 开窗函数示例

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

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