Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ue4Is-004eGQ-Mx for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 04:08:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ue4Ir-004mul-DY for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 04:08:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ue4Ir-004muc-3x for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 04:08:33 +0000 Received: from buffalo.ash.relay.mailchannels.net ([23.83.222.24]) by makus.postgresql.org with smtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1ue4Io-0008Qp-2j for pgsql-hackers@postgresql.org; Tue, 22 Jul 2025 04:08:32 +0000 X-Sender-Id: dreamhost|x-authsender|nico@cryptonector.com Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 2FF9A8C3C0A; Tue, 22 Jul 2025 04:08:29 +0000 (UTC) Received: from pdx1-sub0-mail-a244.dreamhost.com (100-119-222-212.trex-nlb.outbound.svc.cluster.local [100.119.222.212]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id C4DC28C3F39; Tue, 22 Jul 2025 04:08:28 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1753157308; a=rsa-sha256; cv=none; b=K5o+5z5kYd1ycSH2WZB8k5qySWb0S70ZbgB4B6mT9M/VJ4u5ntqapKsvGwlw9RWGg342LK ill2oKMNpuGeLBaFNXtlIEPbkszgGaPFZceYj3panB+/TWxwi1mQJxJq3OTO1bPxhxvBKV f1mhow69ghpuJKyNoGqBomg7j24vHBo3jVCa3ezvaEazbMUpU2q9dno0ikQ53KEMRojOMY 3aVveeG8U+gMvFBIIXgUGVdIW5QB2FN4SsP3OUO+dpRYKJ4ji2f34HUChqUp0aCRBrJb2E SVenMgwWfD0uUWi7hFsf++Gstw9I16QMGPKq3WVxIu9/3gOHXk7VqbF3TiGWCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1753157308; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LHCTF0YhUbZZElJjVST3pMG04qoIGi+R6QEi0z1dK40=; b=JeNuc/41oRdb3CI7Au9psfCeX5eRK/S0laZyy52tC7TiY49NAxb3v2Ig9VyDYzWhXLY2+H 6JkFwHLITlH4mawxDBK/pLbC3Qi5oH1VtGwKyVjqZ7zVA7wYOXCOLq6KvWIjGxIov6E5x0 070M45jRSOH4LlDgnbAq1U7g49azBE/V44Yf2mIi6uQRi4l20HeX2xBAdjJNBGVHkLg1YX IaDlSMWoC499lrp7GxJY5PNmO4jw+u3LtQfds2rc8nX7SR98PurRBLzE7utDKEJ+zMqlcF 5uPAUknNNxX1TFCxtQrTpEMQZ+5qzyk/HYG8mCZmVtzsmZGyVHKzplZNYGj5TQ== ARC-Authentication-Results: i=1; rspamd-7df5bc8bc6-bmkjh; auth=pass smtp.auth=dreamhost smtp.mailfrom=nico@cryptonector.com X-Sender-Id: dreamhost|x-authsender|nico@cryptonector.com X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|nico@cryptonector.com X-MailChannels-Auth-Id: dreamhost X-Soft-Towering: 64361ddf084c346d_1753157309068_2773630625 X-MC-Loop-Signature: 1753157309068:3231747682 X-MC-Ingress-Time: 1753157309068 Received: from pdx1-sub0-mail-a244.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.119.222.212 (trex/7.1.3); Tue, 22 Jul 2025 04:08:29 +0000 Received: from ubby (syn-075-081-095-064.res.spectrum.com [75.81.95.64]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: nico@cryptonector.com) by pdx1-sub0-mail-a244.dreamhost.com (Postfix) with ESMTPSA id 4bmNzb6Nsyz2D; Mon, 21 Jul 2025 21:08:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cryptonector.com; s=dreamhost; t=1753157308; bh=LHCTF0YhUbZZElJjVST3pMG04qoIGi+R6QEi0z1dK40=; h=Date:From:To:Cc:Subject:Content-Type:Content-Transfer-Encoding; b=UqiJvh9cR2G1CajRt8RDGWwMm53IxIr5QAsx90+KzH7yoeTvnZSBZUBToaY4ex7wk pSPVt4ut+il4i7qtj9oibk1fTvUC6aeLy5VSWNR1Gad7zhIV0PXzHgrYS4NbFV9ceL FhuudJ4yRlq1vaLQwMv/8T7jQNXXnMI5q8/s5Pv84WNFgCLuVErW+P9Lp0TkBR+K+u JOshxRrG11Tny6ciIUbXvO+Wl64UjhWTVE8cs+zcfL/PYuaqwhjBTDPKwmwMHeUHGm poNgTA0lVMkcbq+YY0w9A63XGoT7oEe6s/Sk211G/VXjf77Lw3Vu4r+as1HvRxDuIy 2V1GV2roomRIA== Date: Mon, 21 Jul 2025 23:08:25 -0500 From: Nico Williams To: Merlin Moncure Cc: Pavel Stehule , Vik Fearing , Matheus Alcantara , Pg Hackers , Peter Eisentraut Subject: Re: Proposal: QUALIFY clause Message-ID: References: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Jul 21, 2025 at 09:43:15PM -0600, Merlin Moncure wrote: > On Mon, Jul 21, 2025 at 9:19=E2=80=AFPM Pavel Stehule > wrote: > > just for curiosity - why the HAVING clause was not used? > > > > Any window functions are +/- an "aggregate" function, and then HAVING > > looks more natural to me. >=20 > Hm, HAVING requires to apply 'group by' which windows functions do not > require (unlike aggregates). Pavel's point is precisely to allow HAVING w/o a GROUP BY when there are window functions since window functions are "+/-" ("more or less") aggregate functions. That makes sense to me. > superuser@postgres=3D# select * from (select 1 as v) q having true limi= t 1; > ERROR: column "q.v" must appear in the GROUP BY clause or be used in a= n > aggregate function > LINE 1: select * from (select 1 as v) q having true limit 1; >=20 > If a query has both window function and grouped aggregate, HAVING would= be > applying at different grains potentially? If so, seems sus. I would have a HAVING clause that comes _before_ GROUP BY apply to window functions and a second one that comes _after_ GROUP BY apply to the grouping. Nico --=20