Re: SQL ERROR subquery uses ungrouped column in PostgreSQL

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: PALAYRET Jacques <jacques(dot)palayret(at)meteo(dot)fr>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: SQL ERROR subquery uses ungrouped column in PostgreSQL
Date: 2025-04-30 13:09:05
Message-ID: CAApHDvpxHjkguu2zs3Fv2okrgvKzT27ao5CXfyVn7xhXc5HWLw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 1 May 2025 at 00:29, PALAYRET Jacques <jacques(dot)palayret(at)meteo(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Markus Demleitner 2025-04-30 15:16:47 Index not used in certain nested views but not in others
Previous Message PALAYRET Jacques 2025-04-30 12:29:11 SQL ERROR subquery uses ungrouped column in PostgreSQL