Re: BUG #12228: Primary use-case of PERCENT_RANK not supported - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #12228: Primary use-case of PERCENT_RANK not supported
Date
Msg-id 8865.1418599857@sss.pgh.pa.us
Whole thread Raw
In response to BUG #12228: Primary use-case of PERCENT_RANK not supported  (jonlachlan@gmail.com)
Responses Re: BUG #12228: Primary use-case of PERCENT_RANK not supported
List pgsql-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

pgsql-bugs by date:

Previous
From: jonlachlan@gmail.com
Date:
Subject: BUG #12228: Primary use-case of PERCENT_RANK not supported
Next
From: Andrew Sackville-West
Date:
Subject: Re: regression, deadlock in high frequency single-row UPDATE