Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aMneV-00043d-BF for pgsql-bugs@arkaria.postgresql.org; Sat, 23 Jan 2016 02:10:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aMneU-0006oD-FE for pgsql-bugs@arkaria.postgresql.org; Sat, 23 Jan 2016 02:10:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aMneT-0006mz-Nh for pgsql-bugs@postgresql.org; Sat, 23 Jan 2016 02:10:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aMneM-0003sm-7q for pgsql-bugs@postgresql.org; Sat, 23 Jan 2016 02:10:09 +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 u0N29x39022013; Fri, 22 Jan 2016 21:09:59 -0500 From: Tom Lane To: Prateek Sanyal cc: pgsql-bugs@postgresql.org Subject: Re: Is this a bug? In-reply-to: References: Comments: In-reply-to Prateek Sanyal message dated "Fri, 22 Jan 2016 18:58:04 -0500" Date: Fri, 22 Jan 2016 21:09:59 -0500 Message-ID: <22012.1453514999@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 Prateek Sanyal writes: > I discovered a difference between sqlite3 and psql and I am not sure if it > is intentional but it is definitely inconvenient. > If you use an alias for an aggregate function and then do a GROUP BY with > HAVING, you can't use that alias in the HAVING and you have to type in the > entire function again. > This is not the case with sqlite3 where you can just use the alias. Why > doesn't psql allow this? Because it is contrary to both the letter and the spirit of the SQL standard. The SELECT's result list is logically computed after every operation except ORDER BY, so it's really not sensible for clauses like GROUP BY or HAVING to refer to outputs of the SELECT list. What's more, it's ambiguous, because the same name might mean different things depending on whether you consider it to be an input column name or a result column name. PG deviates from the spec to the extent of allowing GROUP BY items to be output column names *as long as they are just that, and not expressions*. (To my mind, that's probably a long-ago mistake, but it's handy enough that there's never been any serious move to remove it.) Even if we applied that policy to HAVING, which we don't, your example wouldn't work because the reference is within an expression. > SELECT SUM(total_sale) AS sum_sales FROM invoices GROUP BY customer_id > HAVING SUM(total_sale) > 20 ORDER BY sum_sales DESC; This code is correct per spec and should work in any SQL DBMS. At least as far as PG is concerned, there's no performance penalty, since the common SUM() expression is evaluated only once anyway. 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