Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UfaNb-0007ZF-7y for pgsql-sql@arkaria.postgresql.org; Thu, 23 May 2013 18:36:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UfaNa-0000v8-KB for pgsql-sql@arkaria.postgresql.org; Thu, 23 May 2013 18:36:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UfaNZ-0000v2-O0 for pgsql-sql@postgresql.org; Thu, 23 May 2013 18:36:45 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UfaNN-00056J-F7 for pgsql-sql@postgresql.org; Thu, 23 May 2013 18:36:45 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1UfaNK-0004oE-Bh for pgsql-sql@postgresql.org; Thu, 23 May 2013 11:36:30 -0700 Date: Thu, 23 May 2013 11:36:30 -0700 (PDT) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1369334190307-5756661.post@n5.nabble.com> In-Reply-To: References: Subject: Re: Select statement with except clause MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 1.8 (+) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org 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