Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y0Icz-0002DY-9N for pgsql-bugs@arkaria.postgresql.org; Sun, 14 Dec 2014 23:31:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y0Icy-0003FP-GX for pgsql-bugs@arkaria.postgresql.org; Sun, 14 Dec 2014 23:31:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y0Icx-0003FI-IJ for pgsql-bugs@postgresql.org; Sun, 14 Dec 2014 23:31:03 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y0Icu-0007NG-Fe for pgsql-bugs@postgresql.org; Sun, 14 Dec 2014 23:31:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id sBENUvSs008866; Sun, 14 Dec 2014 18:30:57 -0500 From: Tom Lane To: jonlachlan@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #12228: Primary use-case of PERCENT_RANK not supported In-reply-to: <20141213221134.2699.23317@wrigleys.postgresql.org> References: <20141213221134.2699.23317@wrigleys.postgresql.org> Comments: In-reply-to jonlachlan@gmail.com message dated "Sat, 13 Dec 2014 22:11:34 +0000" Date: Sun, 14 Dec 2014 18:30:57 -0500 Message-ID: <8865.1418599857@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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