Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lFgA5-0005Ui-OW for pgsql-bugs@arkaria.postgresql.org; Fri, 26 Feb 2021 16:40:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lFgA4-00076Q-Mb for pgsql-bugs@arkaria.postgresql.org; Fri, 26 Feb 2021 16:40:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lFgA4-00076H-GH for pgsql-bugs@lists.postgresql.org; Fri, 26 Feb 2021 16:40:16 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lFgA2-0005fK-Hk for pgsql-bugs@lists.postgresql.org; Fri, 26 Feb 2021 16:40:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 11QGeDU0287739; Fri, 26 Feb 2021 11:40:13 -0500 From: Tom Lane To: dan.farlow@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16900: SELECT MAX subquery not allowed in WHERE clause when using WITH RECURSIVE In-reply-to: <16900-5b4d30739ea178c7@postgresql.org> References: <16900-5b4d30739ea178c7@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Fri, 26 Feb 2021 11:30:57 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <287737.1614357613.1@sss.pgh.pa.us> Date: Fri, 26 Feb 2021 11:40:13 -0500 Message-ID: <287738.1614357613@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > CREATE TABLE IF NOT EXISTS > Tasks (task_id int, subtasks_count int); > ... > UNION ALL > SELECT > task_id, max_subtask_count, subtask_id + 1 > FROM > possible_tasks_subtasks > -- using SELECT MAX in the WHERE clause below is what causes the error > WHERE > subtask_id < (SELECT MAX(max_subtask_count) FROM Tasks)) max_subtask_count is not a field of Tasks; perhaps you meant to write MAX(subtasks_count)? As the query is written, the aggregate is over a field of possible_tasks_subtasks, making it illegal in WHERE, just as the error says. (From the point of view of the "SELECT FROM Tasks" subquery, it's a constant outer reference, not an aggregate of that subquery. This is per SQL spec.) regards, tom lane