pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: jonlachlan@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #12228: Primary use-case of PERCENT_RANK not supported
Date: Sun, 14 Dec 2014 18:30:57 -0500
Message-ID: <8865.1418599857@sss.pgh.pa.us> (raw)
In-Reply-To: <20141213221134.2699.23317@wrigleys.postgresql.org>
References: <20141213221134.2699.23317@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

jonlachlan@gmail.com writes:
> I'm using the 9.4 RC1 and trying out the WITHIN GROUP functions. I used a
> percentile_disc pattern successfully, however I think I came across a
> problem with using percent_rank(). Here is my SQL:

> SELECT percent_rank(datavalue) WITHIN GROUP (ORDER BY datavalue) as
> pct_rank, measureid
>  FROM measuredata
>  WHERE surveyyear=2013
>  GROUP BY measureid;

That query isn't very sensible: the direct argument of percent_rank() has
to be a constant over any one aggregation group, else the percentile
calculation is meaningless.

> I want to be able to run a query that programmatically displays the
> 'pct_rank' of datavalue.

I'm not 100% sure what you mean by that, but I suspect you are looking for
something closer to the basic percent_rank() window function, not the
hypothetical-set function.  Hypothetical-set functions are for computing
the measure that would be attributed to a row that's not actually present
in the data.  Moreover, since they're aggregates, they produce only one
output per GROUP BY group, and I don't understand what you mean by
"pct_rank of datavalue" at a group level.  Perhaps what you want is

SELECT percent_rank() OVER (PARTITION BY measureid ORDER BY datavalue) as                
pct_rank, measureid
 FROM measuredata
 WHERE surveyyear=2013;

It could also be that what you're after will require doing a window
function like that in a sub-SELECT, and then grouping and/or aggregating
in the outer query.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (3+ messages)  latest in thread

Message-ID: <8865.1418599857@sss.pgh.pa.us>
Permalink:  ../8865.1418599857@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/8865.1418599857@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jonlachlan@gmail.com
  Subject: Re: BUG #12228: Primary use-case of PERCENT_RANK not supported
  In-Reply-To: <8865.1418599857@sss.pgh.pa.us>
  References: <20141213221134.2699.23317@wrigleys.postgresql.org> <8865.1418599857@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox