percentile_cont from array?

From: Wells Oliver <wells(dot)oliver(at)gmail(dot)com>
To: pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: percentile_cont from array?
Date: 2023-09-11 20:01:18
Message-ID: CAOC+FBVRp5J4=tCfbGRW3spHvtxJijKzU37wx_0Zofj9PTH3=Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

I find myself needing to get percentile_cont values from array types and
thus did this, wondering if anyone has any "you idiot that's available out
of the box here" kind of feedback?

create or replace function public.percentile_array(numeric, numeric[])
returns real
language 'sql'
immutable
as $$
with u as (
select unnest($2) as i
)
select percentile_cont($1) within group (order by i asc) from u;
$$;

alter function public.percentile_array owner to postgres;

select public.percentile_array(0.9, array[1,2,3,4,5]); -- 4.6

--
Wells Oliver
wells(dot)oliver(at)gmail(dot)com <wellsoliver(at)gmail(dot)com>

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Wells Oliver 2023-09-11 21:26:22 pg_stat_statements_info
Previous Message Nikhil Ingale 2023-09-11 14:09:28 replorigin_checkpoint