Home > Software engineering >  Quartiles in Postgres
Quartiles in Postgres

Time:11-19

I have payment amounts like so -

id      payment
13743   2090439.11
1750327 514457.45
124049  1583176.43
2323    948421.5
1106    2334.2

How do I divide records like this into quartiles. It doesn't have to be perfect. For example, the data in the question would transform like so -

id      payment     Quartile
13743   2090439.11  Q4
1750327 514457.45   Q2
124049  1583176.43  Q3
2323    1232336.5   Q3
1106    2334.2      Q1

Ids 124049 and 2323 are given the same quartile because those payment values are close together.

CodePudding user response:

you can use ntile function :

select * , ntile(4) over (order by payment) as Quartile
from data

db<>fiddle here

  • Related