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.96) (envelope-from ) id 1vvHJ6-00BgqV-1h for pgsql-general@arkaria.postgresql.org; Wed, 25 Feb 2026 16:00:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vvHJ5-006zhV-1b for pgsql-general@arkaria.postgresql.org; Wed, 25 Feb 2026 16:00:11 +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.96) (envelope-from ) id 1vvHJ4-006zhM-29 for pgsql-general@lists.postgresql.org; Wed, 25 Feb 2026 16:00:11 +0000 Received: from fout-b3-smtp.messagingengine.com ([202.12.124.146]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vvHJ1-000000018mi-1BB8 for pgsql-general@lists.postgresql.org; Wed, 25 Feb 2026 16:00:09 +0000 Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfout.stl.internal (Postfix) with ESMTP id 4B8A11D001ED; Wed, 25 Feb 2026 11:00:07 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Wed, 25 Feb 2026 11:00:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1772035207; x=1772121607; bh=VklpDaCgJTiOtivY2Elre8a0yd5Lho5Mh9RsNxd6tbM=; b= AccipWt2TRh78dZcIrz6XtkJerbD5TrDoNNrAatrIjgSMAHot/k3y+3R8M7dzvMj FPH+H+RytHRZDs3ANfXlGdaD6T39/gI2Ml5Hy/0FYmycrBF2nGl8aWKcHasEgf1q KsA93v2LiqCOVnGdTW0ZB/jITqPfmJOOiASU3UiE0g68lR2QGpM+008KYI40aMJm MfJTPquk0CxBCcjwO/qHhdACqwLDIFwRNfzAQw+Blltzv1MzmPeRfd7McCUEd0MB uCuBROkCxcOlytZI2TqYtLDc3AqTWTWrKoXKl/kFaDWpNuE8McXbm7kaKDZjuGe2 m8OQrBxky8011lswfT7h9w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1772035207; x=1772121607; bh=V klpDaCgJTiOtivY2Elre8a0yd5Lho5Mh9RsNxd6tbM=; b=a48mmT7ZeKAoBO5An +qhEX0FeFpA5YXFurfrb/WvQB5QSdDD19LFzb4QN39WWBD695KSzssltFCIB86aM mXL9j7II2UeO4UV1B4NZQ5AsTM3FvcFwx9lDyFp9HlH6EItZPYupt/5/VEsLqZI/ N1DJBTwTs9Q8vZmmC4Sius0wFyQ4XjdL6H5lTKjSzl/68+8y7mU8BqJ0Jj09diRn G4bVwbL3Fy58jR57PENLgZfeMiAu+/WqPoAeGcXhytIoX049TgBPOPicFu6iR2s8 u4UOlzOrm4HIbq5rKIRLTSnsC8lyObRBqOHnxHXGZlIf7NNNavAMp0/mJBirCJ23 7tLcg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvgeefhedvucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepkfffgggfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghn ucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqe enucggtffrrghtthgvrhhnpeffleegieefgfevudehtdfhkeeutdffjeevgeffgeejvedt hefgudeiteefheejheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggp rhgtphhtthhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjrggtqhhuvg hsrdhprghlrgihrhgvthesmhgvthgvohdrfhhrpdhrtghpthhtohepphhgshhqlhdqghgv nhgvrhgrlheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 25 Feb 2026 11:00:06 -0500 (EST) Message-ID: <52acd108-2949-4762-80c2-c761662fde73@aklaver.com> Date: Wed, 25 Feb 2026 08:00:03 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: PostgreSQL query with FULL OUTER JOIN and subquery using column of GROUP BY expression ERROR: subquery uses ungrouped column from outer query To: PALAYRET Jacques , pgsql-general@lists.postgresql.org References: <970600119.344834366.1772018068349.JavaMail.zimbra@meteo.fr> Content-Language: en-US From: Adrian Klaver In-Reply-To: <970600119.344834366.1772018068349.JavaMail.zimbra@meteo.fr> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/25/26 3:14 AM, PALAYRET Jacques wrote: > Hello, > > I have 3 tables : t1, t2 and t3 > t1(posx integer, posy integer, dat timestamp, val_grid numeric) >   with Primary Key=(posx, posy, dat) > t2(id integer, x integer, y integer) >   with Primary Key=(id) > t3(id integer, dat timestamp, c1 numeric) >   with Primary Key=(id, dat) > > My (very simplified) query  that doesn't work: > SELECT >   id, to_char(dat, 'YYYYMM'), CASE > FLOOR((TO_NUMBER(TO_CHAR(dat,'DD'),'99')-1)/10) WHEN 0 THEN 1 WHEN 1 > THEN 2 ELSE 3 END::smallint, > avg(val_grid), sum(c1), > *(SELECT col1 FROM another_table WHERE another_table.np=id LIMIT 1)* > FROM >   (SELECT * FROM t1 ) AS t1 > *JOIN* t2  ON (t1.posx=t2.x AND t1.posy=t2.y) > *FULL OUTER JOIN* (SELECT * FROM t3   ) t3 USING(id, dat) > --WHERE ... > *GROUP BY* id, to_char(dat, 'YYYYMM'), CASE > FLOOR((TO_NUMBER(TO_CHAR(dat,'DD'),'99')-1)/10) WHEN 0 THEN 1 WHEN 1 > THEN 2 ELSE 3 END::smallint > ; > ERROR:  subquery uses ungrouped column "t2.id" from outer query > > -> The problem is on the third line (the subquery) : > (SELECT col1 FROM another_table WHERE another_table.np=id LIMIT 1) > > However, If I replace "FULL OUTER" by "*LEFT OUTER*" or by "*RIGHT > OUTER*", *it works*. > > Could you please tell me how to correct the query, to fix this problem? Questions: 1) Why FROM (SELECT * FROM t1 ) AS t1 instead of just FROM t1? Same for JOIN (SELECT * FROM t3 ) t3 2) Why are the field names not table qualified e.g. t2.id, t3.id, etc? 3) What is the desired outcome? > > Regards > ----- Météo-France ----- > PALAYRET Jacques > DCSC/GDC > jacques.palayret@meteo.fr > Fixe : +33 561078319 -- Adrian Klaver adrian.klaver@aklaver.com