pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Johnston <polobo@yahoo.com>
To: pgsql-sql@postgresql.org
Subject: Re: Select statement with except clause
Date: Thu, 23 May 2013 11:36:30 -0700 (PDT)
Message-ID: <1369334190307-5756661.post@n5.nabble.com> (raw)
In-Reply-To: <CAAY=A79+vsQN3zzPC7CtSoJFPxbp-Kf=yxg-hdCd6AEb3i6P5Q@mail.gmail.com>
References: <CAAY=A79+vsQN3zzPC7CtSoJFPxbp-Kf=yxg-hdCd6AEb3i6P5Q@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

JORGE MALDONADO wrote
> How does the EXCEPT work? Do fields should be identical?
> I need the difference to be on the first 3 fields.

Except operates over the entire tuple so yes all fields are evaluated and,
if they all match, the row from the "left/upper" query is excluded.

If you need something different you can use some variation of:
IN
EXISTS
NOT IN
NOT EXISTS

with a sub-query (correlated or uncorrelated as your need dictates).

For example:

SELECT col1, col2, col3, sum(col4)
FROM tbl
WHERE (col1, col2, col3) NOT IN (SELECT col1, col2, col3 FROM tbl2) -- not
correlated
GROUP BY col1, col2, col3

SELECT col1, col2, col3, sum(col4)
FROM tbl
WHERE NOT EXISTS (
  SELECT 1 FROM tbl AS tbl2 WHERE --make sure to alias the sub-query table
if it matches the outer reference
  (tbl.col1, tbl.col2, tbl.col3) = (tbl2.col1, tbl2.col2, tbl2.col3)
) -- correlated; reference "tbl" within the query inside the where clause
GROUP BY col1, col2, col3

I do not follow your example enough to provide a more explicit
example/solution but this should at least help point you in the right
direction.

David J.









--
View this message in context: http://postgresql.1045698.n5.nabble.com/Select-statement-with-except-clause-tp5756658p5756661.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



view thread (4+ messages)  latest in thread

Message-ID: <1369334190307-5756661.post@n5.nabble.com>
Permalink:  ../1369334190307-5756661.post@n5.nabble.com/
Also on:    postgresql.org/message-id/1369334190307-5756661.post@n5.nabble.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: polobo@yahoo.com
  Subject: Re: Select statement with except clause
  In-Reply-To: <1369334190307-5756661.post@n5.nabble.com>

* 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