你可以寫爲使用percentile_cont()
或percentile_disc()
聚合函數:
select district, percentile_cont(0.25) within group (order by sales)
from t
group by district;
不幸的是,Postgres的目前不支持這些作爲窗口的功能:
select id, percentile_cont(0.25) within group (order by sales) over (partition by district)
from t;
所以,你可以使用一個join
:
select t.*, p_25, p_75
from t join
(select district,
percentile_cont(0.25) within group (order by sales) as p_25,
percentile_cont(0.75) within group (order by sales) as p_75
from t
group by district
) td
on t.district = td.district
當我這樣做,我得到以下錯誤: OVER不支持有序集總PERCENTILE_CONT ...這是奇怪的,因爲我的Postgres 9.5,我想成爲支持9.4? –
@StephenSmith。 。 。文檔 - 如果您仔細閱讀 - 明確指出百分位數函數只是聚合函數而不是窗口函數。 –