Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UfwWu-0004Eo-On for pgsql-sql@arkaria.postgresql.org; Fri, 24 May 2013 18:15:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UfwWu-0000P4-9b for pgsql-sql@arkaria.postgresql.org; Fri, 24 May 2013 18:15:52 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UfwWt-0000Oz-HO for pgsql-sql@postgresql.org; Fri, 24 May 2013 18:15:51 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UfwWr-0004VD-8f for pgsql-sql@postgresql.org; Fri, 24 May 2013 18:15:50 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1UfwWq-0001Yo-HN for pgsql-sql@postgresql.org; Fri, 24 May 2013 11:15:48 -0700 Date: Fri, 24 May 2013 11:15:48 -0700 (PDT) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1369419348510-5756790.post@n5.nabble.com> In-Reply-To: References: <1369334190307-5756661.post@n5.nabble.com> 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: 4.5 (++++) 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 Reply-all is acceptable; but standard list protocol is to respond at the end of the message after performing "quote editing". JORGE MALDONADO wrote > Firstly, I want to thank you for responding. > Secondly, I wonder if I should only reply to the mailing list (I clicked > Reply All); if this is the case, I apologize for any inconvenience. Please > let me know so I reply correctly next time. > > I will describe my issue with more detail. I need to perform 2 very > similar > queries as follows: > > *** QUERY 1 *** > SELECT fldA, fldB, fldC, SUM(fldD) AS fldD > FROM tableA > WHERE condition1 > GROUP BY fldA, fldB, fldC > > *** QUERY 2 *** > SELECT fldA, fldB, fldC, SUM(fldD)*(-1) AS fldD > FROM tableA > WHERE condition2 > GROUP BY fldA, fldB, fldC > > As you can see, both reference the same table and the same fields. > > The differences between the queries are: > a) The last SELECTED field is multiplied by (-1) in the second query. > b) The WHERE conditions. > > What I finally need is to exclude records generated by QUERY1 from QUERY2 > when fldA, fldB and fldC are equal in both results. Example query layout; not promising it is the most efficient but it works. WITH q1 AS ( SELECT fldA, fldB, fldC, sum(fldD) AS sumD ... ) , q2 AS ( SELECT fldA, fldB, fldC, sum(fldD)*-1 AS sumD ... WHERE ... AND (fldA, fldB, fldC) NOT IN (SELECT (q1.fldA, q2.fldB, q3.fldC) FROM q1) ) SELECT fldA, fldB, fldC, sumD FROM q1 UNION ALL SELECT fldA, fldB, fldC, sumD FROM q2 ; If you actually explain the goal and not just ask a technical question you might find that people suggest alternatives that you are not even considering. SELECT fldA, fldB, fldC, sum_positive, sum_negative FROM (SELECT fldA, fldB, fldC, sum(fldD) AS sum_positive) q1 NATURAL FULL OUTER JOIN (SELECT fldA, fldB, fldC, sum(fldD)*-1 AS sum_negative) q2 WHERE <...> Food for thought. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Select-statement-with-except-clause-tp5756658p5756790.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