Re: percentile rank query

From: "William Temperley" <willtemperley(at)gmail(dot)com>
To: "Osvaldo Rosario Kussama" <osvaldo(dot)kussama(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: percentile rank query
Date: 2008-04-10 16:20:21
Message-ID: 439dc11e0804100920l6fb8dcawc8189623c2d99ca0@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Apr 10, 2008 at 4:36 PM, Osvaldo Rosario Kussama
<osvaldo(dot)kussama(at)gmail(dot)com> wrote:
>
> Try:
>
> SELECT count(*) AS frequency, score,
> count((SELECT * FROM scoretable st2 WHERE st2.score <= st1.score)) AS
> runningtotal
> FROM scoretable st1
> GROUP BY score
> ORDER BY score
>
> Osvaldo
>

Thankyou Osvaldo- that worked!

Final version:

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.

Cheers

Will

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Lavoie 2008-04-10 17:55:06 ODBCng vs psqlODBC
Previous Message Tom Lane 2008-04-10 15:57:25 Re: select statement fails