Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 10421 invoked from network); 10 Sep 2010 09:49:09 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 10 Sep 2010 09:49:09 -0000 Received: (qmail 23731 invoked by uid 500); 10 Sep 2010 09:49:09 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 23375 invoked by uid 500); 10 Sep 2010 09:49:06 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 23368 invoked by uid 99); 10 Sep 2010 09:49:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Sep 2010 09:49:05 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of chris@aptivate.org designates 87.106.150.205 as permitted sender) Received: from [87.106.150.205] (HELO one-mail.aptivate.org) (87.106.150.205) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Sep 2010 09:48:58 +0000 Received: from one-mail.aptivate.org ([87.106.150.205] helo=mail.aptivate.org) by one-mail.aptivate.org with esmtps (TLSv1:AES256-SHA:256) (Exim 4.72) (envelope-from ) id 1Ou0Dk-0004u5-AL for derby-user@db.apache.org; Fri, 10 Sep 2010 10:48:36 +0100 Date: Fri, 10 Sep 2010 10:48:36 +0100 (BST) From: Chris Wilson To: Derby Discussion Subject: Cannot use column alias in HAVING clause Message-ID: User-Agent: Alpine 2.00 (LRH 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII Hi all, I'd like to be able to use a column alias (from the column list of the SELECT statement) in the HAVING clause. When I try, I get the following error: ij> select 0 as foo from rita.request having foo; ERROR 42X04: Column 'FOO' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'FOO' is not a column in the target table. Of course, this is a trivial example, but in my real-life case I have an aggregate function being given an alias: SELECT ... SUM(mo2_.ltu_qty) as req_line_ledger_balance ... GROUP BY ... HAVING (req_line_ledger_balance <> 0) If I include the SUM(...) expression again in the HAVING clause, presumably Derby will evaluate it a second time, not knowing that it's already evaluated it and the result is in the selected columns? I understand the purpose of the restriction and the error message, "appears in a HAVING clause and is not in the GROUP BY list," but aggregate functions do not have to be in the GROUP BY list in order to be available in time for HAVING to operate on them, as the values are in the output set. Derby clearly knows this, as using the expression instead of the alias works: ij> select sum(ltu_qty) AS sum_ltu_qty from rita.movement group by move_site_id having sum(ltu_qty) > 0; SUM_LTU_QTY ----------- 0 rows selected ij> select sum(ltu_qty) AS sum_ltu_qty from rita.movement group by move_site_id having sum_ltu_qty > 0; ERROR 42X04: Column 'SUM_LTU_QTY' ... appears in a HAVING clause and is not in the GROUP BY list... I would try to write a patch myself, but I'd need some help to get Derby to compile in Eclipse so that I can debug it. Cheers, Chris. -- Aptivate | http://www.aptivate.org | Phone: +44 1223 760887 The Humanitarian Centre, Fenner's, Gresham Road, Cambridge CB1 2ES Aptivate is a not-for-profit company registered in England and Wales with company number 04980791.