Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y0D41-0007Xm-CK for pgsql-bugs@arkaria.postgresql.org; Sun, 14 Dec 2014 17:34:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y0D40-0002Es-RH for pgsql-bugs@arkaria.postgresql.org; Sun, 14 Dec 2014 17:34:36 +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 1Xzuv0-0001ZM-16 for pgsql-bugs@postgresql.org; Sat, 13 Dec 2014 22:12:06 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xzuuw-0005Pf-RP for pgsql-bugs@postgresql.org; Sat, 13 Dec 2014 22:12:04 +0000 MIME-Version: 1.0 Subject: BUG #12228: Primary use-case of PERCENT_RANK not supported To: pgsql-bugs@postgresql.org From: jonlachlan@gmail.com Date: Sat, 13 Dec 2014 22:11:34 +0000 Message-ID: <20141213221134.2699.23317@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.3 (/) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 The following bug has been logged on the website: Bug reference: 12228 Logged by: Jonathon Lachlan-Hache Email address: jonlachlan@gmail.com PostgreSQL version: 9.4beta2 Operating system: MacOSX Description: 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; I want to be able to run a query that programmatically displays the 'pct_rank' of datavalue. My table is organized into four columns: organizationid measureid surveyyear datavalue The 'datavalue' column is the measure, whereas all the other columns are attributes. When I run percent_rank() it needs to determine what percentile is the 'datavalue', but within the scope of the same 'measureid' and 'surveyyear'. The percent_rank() function does not appear to support this. The above query returns the following: ERROR: column "measuredata.datavalue" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT percent_rank(datavalue) WITHIN GROUP (ORDER BY datava... ^ DETAIL: Direct arguments of an ordered-set aggregate must use only grouped columns. ********** Error ********** ERROR: column "measuredata.datavalue" must appear in the GROUP BY clause or be used in an aggregate function SQL state: 42803 Detail: Direct arguments of an ordered-set aggregate must use only grouped columns. Character: 21 This doesn't make sense, because if I include 'datavalue' as a GROUP BY clause, then my group N-size is exactly 1. Basically, because the argument of percent_rank() must also be present in the GROUP BY clause, there is no way to use percent_rank() to programatically determine the percent-rank of a value within a set. And this is likely to be the primary use-case of any kind of percent-rank function, so it would make sense to include it in the new implementation. Thanks so much, Jon -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs