public inbox for [email protected]  
help / color / mirror / Atom feed
Re: Syntax error needs explanation
6+ messages / 3 participants
[nested] [flat]

* Re: Syntax error needs explanation
@ 2025-07-14 19:36 Adrian Klaver <[email protected]>
  2025-07-14 19:45 ` Re: Syntax error needs explanation Adrian Klaver <[email protected]>
  0 siblings, 1 reply; 6+ messages in thread

From: Adrian Klaver @ 2025-07-14 19:36 UTC (permalink / raw)
  To: Rich Shepard <[email protected]>; [email protected]

On 7/14/25 12:12, Rich Shepard wrote:
> I have the following script:
> 
> select c.company_nbr, c.company_name, i.industry, from companies as c, 
> industry as i, enforcement as e
> where exists (
>    select c.company_nbr, count(e.action_date), sum(e.penalty_amt)
>    from e.enforcement
>    where c.company_nbr = e.company_nbr
>    )
> group by industry
> order by industry;
> 
> When I run it psql reports an error:
> psql:companies-with-enforcement-actions.txt:127: ERROR:  syntax error at 
> or near "company_nbr"
> LINE 1: company_nbr |               company_name

The above looks like the format 'aligned' output from a query.

When you did \0 you captured that.

As example:

production=# \o test.sql
production=# select * from cell_per;
production=# \e test.sql

line_id |  category  | cell_per |      ts_insert      |      ts_update 
    | user_insert | user_update | plant_type |   season   | short_category

[...]

>          ^
> and I'm not seeing the error. What am I missing?
> 
> TIA,
> 
> Rich
> 
> 

-- 
Adrian Klaver
[email protected]







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Syntax error needs explanation
  2025-07-14 19:36 Re: Syntax error needs explanation Adrian Klaver <[email protected]>
@ 2025-07-14 19:45 ` Adrian Klaver <[email protected]>
  2025-07-14 19:56   ` Re: Syntax error needs explanation David G. Johnston <[email protected]>
  2025-07-14 19:59   ` Re: Syntax error needs explanation Rich Shepard <[email protected]>
  0 siblings, 2 replies; 6+ messages in thread

From: Adrian Klaver @ 2025-07-14 19:45 UTC (permalink / raw)
  To: Rich Shepard <[email protected]>; +Cc: pgsql-general

On 7/14/25 12:38, Rich Shepard wrote:

Reply to list also.
Ccing list.

> On Mon, 14 Jul 2025, Adrian Klaver wrote:
> 
>> The above looks like the format 'aligned' output from a query.
> 
> Adrian,
> 
> I mistyped the script's extension as .txt rather than .sql.

That does not matter, that is more for user convenience in figuring out 
what the files are for.

\o test.txt
select * from cell_per;

Then doing:

psql -d production -U postgres -h localhost -p 5432 -f test.sql
Null display is "NULL".
psql:test.sql:82: ERROR:  syntax error at or near "line_id"
LINE 1: line_id |  category  | cell_per |      ts_insert      |

psql -d production -U postgres -h localhost -p 5432 -f test.txt
Null display is "NULL".
psql:test.txt:82: ERROR:  syntax error at or near "line_id"
LINE 1: line_id |  category  | cell_per |      ts_insert      |     ...

The error is the same as psql just processes the file without regard to 
the extension.

> 
> Regards,
> 
> Rich

-- 
Adrian Klaver
[email protected]







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Syntax error needs explanation
  2025-07-14 19:36 Re: Syntax error needs explanation Adrian Klaver <[email protected]>
  2025-07-14 19:45 ` Re: Syntax error needs explanation Adrian Klaver <[email protected]>
@ 2025-07-14 19:56   ` David G. Johnston <[email protected]>
  1 sibling, 0 replies; 6+ messages in thread

From: David G. Johnston @ 2025-07-14 19:56 UTC (permalink / raw)
  To: Adrian Klaver <[email protected]>; +Cc: Rich Shepard <[email protected]>; pgsql-general

On Mon, Jul 14, 2025 at 12:45 PM Adrian Klaver <[email protected]>
wrote:

> >
> > I mistyped the script's extension as .txt rather than .sql.
>
> That does not matter, that is more for user convenience in figuring out
> what the files are for.
>
>
I think all that is being said is the error was a simple typo, choosing the
wrong file to execute.  Not reading the error message was the real issue,
not a failure to understand how psql scripting works.

David J.


^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Syntax error needs explanation
  2025-07-14 19:36 Re: Syntax error needs explanation Adrian Klaver <[email protected]>
  2025-07-14 19:45 ` Re: Syntax error needs explanation Adrian Klaver <[email protected]>
@ 2025-07-14 19:59   ` Rich Shepard <[email protected]>
  2025-07-14 20:07     ` Re: Syntax error needs explanation David G. Johnston <[email protected]>
  1 sibling, 1 reply; 6+ messages in thread

From: Rich Shepard @ 2025-07-14 19:59 UTC (permalink / raw)
  To: pgsql-general

On Mon, 14 Jul 2025, Adrian Klaver wrote:

> That does not matter, that is more for user convenience in figuring out what 
> the files are for.

Adrian,

Okay.

I still have issues with the script. I want a count of companies with
regulatory enforcement actions by industry. Since not all regulated
companies have had such actions I want only those with rows in the
enforcemewnt table and haven't before used the EXISTS operator and a
subquery.

The current version of the script:

select c.company_nbr, c.company_name, c.industry
from companies as c
where exists (
   select e.company_nbr
   from enforcement as e
   )
group by c.industry
order by c.industry;

And psql tells me that c.company_nbr must be in the group by clause.
However, when I do that the output is a list of company numbers and names in
each industry.

My web searches on using the exists operator haven't provided the knowlege
for me to use it properly.

Rich






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Syntax error needs explanation
  2025-07-14 19:36 Re: Syntax error needs explanation Adrian Klaver <[email protected]>
  2025-07-14 19:45 ` Re: Syntax error needs explanation Adrian Klaver <[email protected]>
  2025-07-14 19:59   ` Re: Syntax error needs explanation Rich Shepard <[email protected]>
@ 2025-07-14 20:07     ` David G. Johnston <[email protected]>
  2025-07-14 20:13       ` Re: Syntax error needs explanation [RESOLVED] Rich Shepard <[email protected]>
  0 siblings, 1 reply; 6+ messages in thread

From: David G. Johnston @ 2025-07-14 20:07 UTC (permalink / raw)
  To: Rich Shepard <[email protected]>; +Cc: pgsql-general

On Mon, Jul 14, 2025 at 12:59 PM Rich Shepard <[email protected]>
wrote:

>
> The current version of the script:
>
> select c.company_nbr, c.company_name, c.industry
> from companies as c
> where exists (
>    select e.company_nbr
>    from enforcement as e
>    )
> group by c.industry
> order by c.industry;
>
> And psql tells me that c.company_nbr must be in the group by clause.
> However, when I do that the output is a list of company numbers and names
> in
> each industry.
>
> My web searches on using the exists operator haven't provided the knowlege
> for me to use it properly.
>
>
Yeah, you need both to read up on aggregate queries and correlated
subqueries which is typically how one makes uses of exists (it's called a
semi-join in this formulation)
Not tested, but:

select c.industry, count(*)
from companies as c
where exists (
select from enforcement as e
where e.company_nbr = c.company_nbr
)
group by c.industry;

David J.


^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Syntax error needs explanation [RESOLVED]
  2025-07-14 19:36 Re: Syntax error needs explanation Adrian Klaver <[email protected]>
  2025-07-14 19:45 ` Re: Syntax error needs explanation Adrian Klaver <[email protected]>
  2025-07-14 19:59   ` Re: Syntax error needs explanation Rich Shepard <[email protected]>
  2025-07-14 20:07     ` Re: Syntax error needs explanation David G. Johnston <[email protected]>
@ 2025-07-14 20:13       ` Rich Shepard <[email protected]>
  0 siblings, 0 replies; 6+ messages in thread

From: Rich Shepard @ 2025-07-14 20:13 UTC (permalink / raw)
  To: pgsql-general

On Mon, 14 Jul 2025, David G. Johnston wrote:

> Yeah, you need both to read up on aggregate queries and correlated
> subqueries which is typically how one makes uses of exists (it's called a
> semi-join in this formulation)

David,

Thanks. I wasn't sure what to read.

> Not tested, but:
>
> select c.industry, count(*)
> from companies as c
> where exists (
> select from enforcement as e
> where e.company_nbr = c.company_nbr
> )
> group by c.industry;

Works as intended.

Many thanks,

Rich






^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2025-07-14 20:13 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-07-14 19:36 Re: Syntax error needs explanation Adrian Klaver <[email protected]>
2025-07-14 19:45 ` Adrian Klaver <[email protected]>
2025-07-14 19:56   ` David G. Johnston <[email protected]>
2025-07-14 19:59   ` Rich Shepard <[email protected]>
2025-07-14 20:07     ` David G. Johnston <[email protected]>
2025-07-14 20:13       ` Re: Syntax error needs explanation [RESOLVED] Rich Shepard <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox