postgres —— 有序集与假想聚集
有序集
-- 有序集,表示在按给定顺序排序后,对数据进行聚合统计 SELECT region, percentile_disc(0.5) WITHIN GROUP (order by production) from t_oil group by region; -- 中位数函数 -- percentile_disc 找相似值; percentile_cont 找精确值,没有则插入 SELECT percentile_disc(0.62) WITHIN GROUP (order by id), percentile_cont(0.62) WITHIN group (order by id) from generate_series(1,5) as id;-- 3 个不同的值出现了正好5次。当然,mode 函数只能给出其中一个 SELECT country, mode() WITHIN GROUP (order by production) from t_oil where country = 'Other Middle East' group by 1;
-- 查找最频繁的值 SELECT production, count(*) from t_oil where country = 'Other Middle East' group by production order by 2 desc limit 4;
假想聚集
-- 假想函数, 用以解决如果一个值在其中将会怎样的问题。 -- 例如使用 rank(9000) 函数, 即表示如果插入一个 production=9000 的新记录,它会在每个 region 组中排名第几? SELECT region, rank(9000) WITHIN GROUP (order by production desc nulls last) from t_oil group by rollup(1)
注:NULLS LAST 表示空值总是排在末尾
PS:测试数据在上一篇
233