Re: percentile rank query

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: percentile rank query
Date: 2008-04-10 18:38:00
Message-ID: 20080410183800.GW6870@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Apr 10, 2008 at 05:20:21PM +0100, William Temperley wrote:
> SELECT count(*) AS frequency, score,
> ((((SELECT count(uid) FROM scoretable st2 WHERE st2.score <=
> st1.score) - count(*)) + (count(*)/2))::float/(select
> count(*) from scoretable))
>
> FROM scoretable st1
> GROUP BY score
> ORDER BY score
>
> I think that's a percentile rank now.

I'm not quite sure how this is calculated but I think you may want to
be converting to a non-integral type earlier (i.e. as you're dividing
by two, not after). I also find all the subselects a bit difficult to
follow so have moved them around:

SELECT x.frequency, x.score, (x.rank + x.frequency / 2.0) / y.total AS pr
FROM (
SELECT count(*) AS frequency, score,
(SELECT count(*) FROM scoretable t WHERE t.score < s.score) AS rank
FROM scoretable s
GROUP BY score) x, (SELECT count(*) AS total FROM scoretable) y
ORDER BY score;

Sam

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Brent Wood 2008-04-10 18:55:41 PG 8.3 review in Linux Mag
Previous Message Ben 2008-04-10 18:36:34 Re: Way to shutdown/freeze/stop an individual database without taking postmaster down?