Thread: SQL ERROR subquery uses ungrouped column in PostgreSQL
Hello,
I have an unexpected error in my following query (of course, the query has been simplified here to request help):
SELECT num_poste, to_char(dat, 'YYYYMM')::integer dat, CASE FLOOR((TO_NUMBER(TO_CHAR(dat,'DD'),'99')-1)/10) WHEN 0 THEN 1 WHEN 1 THEN 2 ELSE 3 END::smallint AS num_decade
, CASE
WHEN ( CASE FLOOR((TO_NUMBER(TO_CHAR(dat,'DD'),'99')-1)/10) WHEN 0 THEN 1 WHEN 1 THEN 2 ELSE 3 END::smallint IN (1 , 2) AND count(glot)=10 )
THEN
(
SELECT valeur
FROM table3
WHERE num_serie_coef=CASE WHEN num_poste <96000000 THEN 0 WHEN num_poste BETWEEN 97100000 and 98899999 THEN num_poste/100000 END
AND coef = 'APRIME_ANGSTROM'
AND num_mois=substr(to_char(dat,'yyyymm'), 5, 2)::smallint
)
ELSE NULL
END AS toto
FROM table1 JOIN table2 USING (num_poste)
GROUP BY num_poste, 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
ORDER BY num_poste, 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 "table1.dat" from outer query
LIGNE 10 : AND num_mois=substr(to_char(dat,'yyyymm'), 5, 2)::smallint
^
=> In the subquery, the semantic analysis of the query considers the column " dat " instead of the expression " to_char(dat,'yyyymm') ", which is actually a grouped column.
Is this normal? How can I simply resolve the problem?
Sincerely.
I have an unexpected error in my following query (of course, the query has been simplified here to request help):
SELECT num_poste, to_char(dat, 'YYYYMM')::integer dat, CASE FLOOR((TO_NUMBER(TO_CHAR(dat,'DD'),'99')-1)/10) WHEN 0 THEN 1 WHEN 1 THEN 2 ELSE 3 END::smallint AS num_decade
, CASE
WHEN ( CASE FLOOR((TO_NUMBER(TO_CHAR(dat,'DD'),'99')-1)/10) WHEN 0 THEN 1 WHEN 1 THEN 2 ELSE 3 END::smallint IN (1 , 2) AND count(glot)=10 )
THEN
(
SELECT valeur
FROM table3
WHERE num_serie_coef=CASE WHEN num_poste <96000000 THEN 0 WHEN num_poste BETWEEN 97100000 and 98899999 THEN num_poste/100000 END
AND coef = 'APRIME_ANGSTROM'
AND num_mois=substr(to_char(dat,'yyyymm'), 5, 2)::smallint
)
ELSE NULL
END AS toto
FROM table1 JOIN table2 USING (num_poste)
GROUP BY num_poste, 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
ORDER BY num_poste, 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 "table1.dat" from outer query
LIGNE 10 : AND num_mois=substr(to_char(dat,'yyyymm'), 5, 2)::smallint
^
=> In the subquery, the semantic analysis of the query considers the column " dat " instead of the expression " to_char(dat,'yyyymm') ", which is actually a grouped column.
Is this normal? How can I simply resolve the problem?
Sincerely.
On Thu, 1 May 2025 at 00:29, PALAYRET Jacques <jacques.palayret@meteo.fr> wrote: > => In the subquery, the semantic analysis of the query considers the column " dat " instead of the expression " to_char(dat,'yyyymm')", which is actually a grouped column. > Is this normal? How can I simply resolve the problem? Calculate the dat column in a subquery in the outer-level query. Something like: FROM (select *,to_char(dat, 'YYYYMM') as strdat from table1) table1 JOIN table2 USING (num_poste) then use strdat in all the places you're currently using to_char(dat, 'YYYYMM') David