pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pinker <pinker@onet.eu>
Cc: pgsql-general@postgresql.org
Subject: Re: except all & WITH - syntax error?
Date: Mon, 02 Jul 2018 11:13:53 -0400
Message-ID: <63780.1530544433@sss.pgh.pa.us> (raw)
In-Reply-To: <1530541501991-0.post@n3.nabble.com>
References: <1530541501991-0.post@n3.nabble.com>

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




view thread (4+ messages)  latest in thread

Message-ID: <63780.1530544433@sss.pgh.pa.us>
Permalink:  ../63780.1530544433@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/63780.1530544433@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pinker@onet.eu
  Subject: Re: except all & WITH - syntax error?
  In-Reply-To: <63780.1530544433@sss.pgh.pa.us>
  References: <1530541501991-0.post@n3.nabble.com> <63780.1530544433@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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