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 1vvCqg-008VeZ-19 for pgsql-general@arkaria.postgresql.org; Wed, 25 Feb 2026 11:14:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vvCqf-005gd8-0t for pgsql-general@arkaria.postgresql.org; Wed, 25 Feb 2026 11:14:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vvCqe-005gcz-2z for pgsql-general@lists.postgresql.org; Wed, 25 Feb 2026 11:14:32 +0000 Received: from zcsmtaf02-pub.meteo.fr ([137.129.63.6]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vvCqb-00000001CxD-13iv for pgsql-general@lists.postgresql.org; Wed, 25 Feb 2026 11:14:32 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by zcsmtaf02-pub.meteo.fr (Postfix) with ESMTP id D6F1940ED6D0 for ; Wed, 25 Feb 2026 11:14:28 +0000 (GMT) Received: from zcsmtaf02-pub.meteo.fr ([127.0.0.1]) by localhost (zcsmtaf02.meteo.fr [127.0.0.1]) (amavis, port 10032) with ESMTP id OXoqjfWuh1yd for ; Wed, 25 Feb 2026 11:14:28 +0000 (GMT) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcsmtaf02-pub.meteo.fr (Postfix) with ESMTP id BDCFB40ED6DA for ; Wed, 25 Feb 2026 11:14:28 +0000 (GMT) X-Virus-Scanned: amavis at meteo.fr Received: from zcsmtaf02-pub.meteo.fr ([127.0.0.1]) by localhost (zcsmtaf02.meteo.fr [127.0.0.1]) (amavis, port 10026) with ESMTP id Xxd-wXW00YUo for ; Wed, 25 Feb 2026 11:14:28 +0000 (GMT) Received: from zcsmsm04.meteo.fr (zcsmsm04.meteo.fr [172.24.3.124]) by zcsmtaf02-pub.meteo.fr (Postfix) with ESMTP id A87CA40ED6D0 for ; Wed, 25 Feb 2026 11:14:28 +0000 (GMT) Date: Wed, 25 Feb 2026 11:14:28 +0000 (GMT) From: PALAYRET Jacques To: pgsql-general@lists.postgresql.org Message-ID: <970600119.344834366.1772018068349.JavaMail.zimbra@meteo.fr> Subject: PostgreSQL query with FULL OUTER JOIN and subquery using column of GROUP BY expression ERROR: subquery uses ungrouped column from outer query MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_421e0b7a-4bb5-4e9d-868c-c8b2862dfabe" X-Originating-IP: [172.24.2.157] X-Mailer: Zimbra 9.0.0_GA_4583 (ZimbraWebClient - FF128 (Win)/9.0.0_GA_4583) Thread-Index: 8hulqi8uEdkVvJbvpLI0ua4YNfyGJw== Thread-Topic: PostgreSQL query with FULL OUTER JOIN and subquery using column of GROUP BY expression ERROR: subquery uses ungrouped column from outer query List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=_421e0b7a-4bb5-4e9d-868c-c8b2862dfabe Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello,=20 I have 3 tables : t1, t2 and t3=20 t1(posx integer, posy integer, dat timestamp, val_grid numeric)=20 with Primary Key=3D(posx, posy, dat)=20 t2(id integer, x integer, y integer)=20 with Primary Key=3D(id)=20 t3(id integer, dat timestamp, c1 numeric)=20 with Primary Key=3D(id, dat)=20 My (very simplified) query that doesn't work :=20 SELECT=20 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,=20 avg(val_grid), sum(c1),=20 (SELECT col1 FROM another_table WHERE another_table.np=3Did LIMIT 1)=20 FROM=20 (SELECT * FROM t1 ) AS t1=20 JOIN t2 ON (t1.posx=3Dt2.x AND t1.posy=3Dt2.y)=20 FULL OUTER JOIN (SELECT * FROM t3 ) t3 USING(id, dat)=20 --WHERE ...=20 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=20 ;=20 ERROR: subquery uses ungrouped column "t2.id" from outer query=20 -> The problem is on the third line (the subquery) :=20 (SELECT col1 FROM another_table WHERE another_table.np=3Did LIMIT 1)=20 However, If I replace " FULL OUTER " by " LEFT OUTER " or by " RIGHT OUTER = ", it works .=20 Could you please tell me how to correct the query, to fix this problem?=20 Regards=20 ----- M=C3=A9t=C3=A9o-France -----=20 PALAYRET Jacques=20 DCSC/GDC=20 jacques.palayret@meteo.fr=20 Fixe : +33 561078319=20 --=_421e0b7a-4bb5-4e9d-868c-c8b2862dfabe Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello,

I have 3 tables : t1, t2 and t3
t1(posx integer, posy integer, dat timestamp, val_grid= numeric)
  with Primary Key=3D(posx, posy, dat)
t2(id integer,= x integer, y integer)
  with Primary Key=3D(id)
t3(id integer, = dat timestamp, c1 numeric)
  with Primary Key=3D(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::sma= llint,
avg(val_grid), sum(c1),
  (SELECT col1 FROM another_table WHERE another_table.np=3Did LI= MIT 1)
FROM
  (SELECT * FROM t1 ) AS t1
  JOIN t2  ON (t1.posx=3Dt2.x AND t1.posy=3Dt2= .y)
  FULL OUTER JOIN (SELECT * FROM t3   ) t3 USING(id, dat)
--WHERE ...=
GROUP BY id, to_char(dat, 'YYYYMM'), CASE FLOOR((TO_NU= MBER(TO_CHAR(dat,'DD'),'99')-1)/10) WHEN 0 THEN 1 WHEN 1 THEN 2 ELSE 3 END:= :smallint
;
ERROR:  subquery uses ungro= uped column "t2.id" from outer query

-> The problem is on = the third line (the subquery) :
(SELECT col1 FROM another_table WHERE an= other_table.np=3Did 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?

Regards
----- M=C3=A9t=C3=A9o-F= rance -----
PALAYRET Jacques
DCSC/GDC
jacques.palayret@meteo.frFixe : +33 561078319
--=_421e0b7a-4bb5-4e9d-868c-c8b2862dfabe--