X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9CD4D475F53 for ; Fri, 4 Apr 2003 17:58:17 -0500 (EST) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 2722F475EFD for ; Fri, 4 Apr 2003 17:58:17 -0500 (EST) Received: by news.hub.org (Postfix, from userid 8) id 0E70543A89E; Fri, 4 Apr 2003 17:58:17 -0500 (EST) From: Lee Harr X-Newsgroups: comp.databases.postgresql.sql Subject: Re: Difficult query (for me) Date: Fri, 4 Apr 2003 22:58:16 +0000 (UTC) Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 78 Message-ID: References: <4ff986f9.0304041206.1ae7b97a@posting.google.com> Reply-To: missive@hotmail.com X-Complaints-To: usenet@news.hub.org User-Agent: slrn/0.9.7.3 (FreeBSD) To: pgsql-sql@postgresql.org X-Spam-Status: No, hits=-28.5 required=5.0 tests=BAYES_10,EMAIL_ATTRIBUTION,QUOTED_EMAIL_TEXT,REFERENCES, REPLY_WITH_QUOTES,USER_AGENT autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/92 X-Sequence-Number: 12793 In article <4ff986f9.0304041206.1ae7b97a@posting.google.com>, Stephane wrote: > Hello, > > Basicaly I have two tables like this: > > Table 1 > RecordName Value > a 100 > b 100 > c 100 > d 100 > e 100 > f 100 > > Table 2 > RecordName Value > a 25 > b 50 > c 75 > g 150 > h 150 > > I would like a query that give me a result like this: > a 75 (100-25) > b 50 (100-50) > c 25 > d 100 > e 100 > f 100 > g -150 > h -150 > > It is table1-table2. > I could do it for records a,b,c but I do not how to have record which > are only in one of the two tables. > Thanks for your help. > Stephane. test=# select * from one; n | v ---+----- a | 100 b | 100 c | 100 d | 100 e | 100 f | 100 (6 rows) test=# select * from two; n | v ---+----- a | 25 b | 50 c | 75 g | 150 h | 150 (5 rows) test=# select coalesce(one.n, two.n), one.v as v1, two.v as v2, test-# coalesce(one.v, 0)-coalesce(two.v, 0) as difference test-# from one full outer join two on (one.n = two.n); case | v1 | v2 | difference ------+-----+-----+------------ a | 100 | 25 | 75 b | 100 | 50 | 50 c | 100 | 75 | 25 d | 100 | | 100 e | 100 | | 100 f | 100 | | 100 g | | 150 | -150 h | | 150 | -150 (8 rows)