Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
Postgres: average of values greater than 90th, 95th, 98th percentile

I have a column in a table with multiple integer records.

I need to write a PostgreSql query which will return the average of all the values which are greater than 90, 95 and 98 percentile.

e.g.

I have series 1-150 in my column.

Now if I take 90th percentile of this column it is around 135.

I need to calculate average of all the values greater than 135.

Similarly for 95 and 98 percentile also.

And if possible all the three values in single query.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

as sample I use numbers from 1 to 300:

t=#  select generate_series(1,300,1) g
 g
---
 1
 2
 3
 4
...

here's an example:

t=# with p as (
 with s as (
  select generate_series(1,300,1) g
 )
 select g,ntile(100) over (order by g)
 , case when ntile(100) over (order by g) between 90 and 94 then 90
   when ntile(100) over (order by g) between 95 and 97 then 95
   when ntile(100) over (order by g) >=98 then 98 end "percentile"
 from s
)
select distinct "percentile",avg(g) over (partition by "percentile")
from p
where ntile >=90;
 percentile |         avg
------------+----------------------
         90 | 275.0000000000000000
         98 | 296.0000000000000000
         95 | 287.0000000000000000
(3 rows)
over 4 years ago · Santiago Trujillo Relatório

0

If I correctly understood, may be you need this.

try:

with t(col) as(
    select * from generate_series(1, 150) 
)

select  
    (select avg(col) from t where col > (select count(*) * 90 / 100.00 from t)), 
    (select avg(col) from t where col > (select count(*) * 95 / 100.00 from t)),
    (select avg(col) from t where col > (select count(*) * 98 / 100.00 from t))
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda