pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
except all & WITH - syntax error?
4+ messages / 3 participants
[nested] [flat]

* except all & WITH - syntax error?
@ 2018-07-02 14:25  pinker <pinker@onet.eu>
  0 siblings, 1 reply; 4+ messages in thread

From: pinker @ 2018-07-02 14:25 UTC (permalink / raw)
  To: pgsql-general

Hi!
Something strange happened to me right now. I'm trying to compare results
from one query with rewritten version and everything is ok with this order:

WITH abc AS (SELECT 1) SELECT 1
except all
SELECT 1

but when I'm trying other way around it throws an error:
SELECT 1
except all
WITH abc AS (SELECT 1) SELECT 1

ERROR: syntax error at or near "WITH" LINE 3: WITH abc AS (SELECT 1) SELECT
1 ^ SQL state: 42601 Character: 21

"PostgreSQL 9.6.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5
20150623 (Red Hat 4.8.5-16), 64-bit"

Is this expected behaviour? 
It's much harder to optimize queries without this option.



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html




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

* Re: except all & WITH - syntax error?
@ 2018-07-02 15:13  Tom Lane <tgl@sss.pgh.pa.us>
  parent: pinker <pinker@onet.eu>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2018-07-02 15:13 UTC (permalink / raw)
  To: pinker <pinker@onet.eu>; +Cc: pgsql-general

pinker <pinker@onet.eu> writes:
> Something strange happened to me right now. I'm trying to compare results
> from one query with rewritten version and everything is ok with this order:

> WITH abc AS (SELECT 1) SELECT 1
> except all
> SELECT 1

> but when I'm trying other way around it throws an error:
> SELECT 1
> except all
> WITH abc AS (SELECT 1) SELECT 1

You need some parens:

# SELECT 1
except all
(WITH abc AS (SELECT 1) SELECT 1);
 ?column? 
----------
(0 rows)

In your first example, the WITH actually attaches to the whole EXCEPT
construct, not the first sub-select as I suspect you're thinking.

In short: WITH has lower syntactic precedence than UNION/INTERSECT/EXCEPT.
You need parens if you want it to work the other way 'round.

			regards, tom lane




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

* Re: except all & WITH - syntax error?
@ 2018-07-03 09:05  pinker <pinker@onet.eu>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: pinker @ 2018-07-03 09:05 UTC (permalink / raw)
  To: pgsql-general

thank you for the answer, had no idea about "syntactic precedence" thing.



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html




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

* Re: except all & WITH - syntax error?
@ 2018-07-03 13:18  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: pinker <pinker@onet.eu>
  0 siblings, 0 replies; 4+ messages in thread

From: Adrian Klaver @ 2018-07-03 13:18 UTC (permalink / raw)
  To: pinker <pinker@onet.eu>; pgsql-general

On 07/03/2018 02:05 AM, pinker wrote:
> thank you for the answer, had no idea about "syntactic precedence" thing.

The order in which commands are executed in the absence of specific 
instructions e.g. the use of parenthesis.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




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


end of thread, other threads:[~2018-07-03 13:18 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-07-02 14:25 except all & WITH - syntax error? pinker <pinker@onet.eu>
2018-07-02 15:13 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-07-03 09:05   ` pinker <pinker@onet.eu>
2018-07-03 13:18     ` Adrian Klaver <adrian.klaver@aklaver.com>

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