Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUvsM-0005BW-Tm for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 15:47:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUvsK-0005TB-Ch for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 15:47:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUvsK-0005T4-0x for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 15:47:40 +0000 Received: from ec2-34-255-138-49.eu-west-1.compute.amazonaws.com ([34.255.138.49] helo=zimbabwe.chezphil.org) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUvsH-0005r1-9o for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 15:47:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=chezphil.org mappendix.com; s=20171026; h=Content-Type:MIME-Version: References:In-Reply-To:Message-ID:Subject:Date:Cc:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=hCxg8dsVP0/safaQMDFIxeuIKEAZ4YW7NjlWuWEVbD8=; b=DjK3bzbRA8iKlK73HlS0TY+Te g33remH4mN0fE4vRjIsUufK+pP6iQLHeZMsc3YTQsPYyS2q3mX2gGRNWfmtL3tdHDiwyn1DoqiG6d qpVIO3iqmYB3pkX6mCKEPyTdackKgB0+hf4Q8wCMqK3PKlsuvOSl2Ih6jsRmH3ata9NeU=; Received: from localhost ([127.0.0.1] helo=chezphil.org) by zimbabwe.chezphil.org with esmtp (Exim 4.89) (envelope-from ) id 1gUvrD-0004X4-0F for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 15:46:31 +0000 Received: from localhost ([::ffff:127.0.0.1]) by zimbabwe with smtp (dmsmtpd 0.00001); Thu, 06 Dec 2018 15:46:31 +0000 From: "Phil Endecott" To: "Ron" Cc: Date: Thu, 06 Dec 2018 15:46:30 +0000 Subject: Re: Limitting full join to one match Message-ID: <1544111190792@dmwebmail.dmwebmail.chezphil.org> In-Reply-To: <5d194157-789b-6e3c-4827-d462f58f0304@gmail.com> References: <5d194157-789b-6e3c-4827-d462f58f0304@gmail.com> X-Mailer: Decimail Webmail 3alpha16 MIME-Version: 1.0 Content-Type: text/plain; format="flowed"; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Ron, Ron wrote: > On 12/05/2018 06:34 PM, Phil Endecott wrote: >> Dear Experts, >> >> I have a couple of tables that I want to reconcile, finding rows >> that match and places where rows are missing from one table or the >> other: >> >> db=> select * from a; >> +------------+--------+ >> | date | amount | >> +------------+--------+ >> | 2018-01-01 | 10.00 | >> | 2018-02-01 | 5.00 | <-- missing from b >> | 2018-04-01 | 5.00 | >> +------------+--------+ >> >> db=> select * from b; >> +------------+--------+ >> | date | amount | >> +------------+--------+ >> | 2018-01-01 | 10.00 | >> | 2018-03-01 | 8.00 | <-- missing from a >> | 2018-04-01 | 5.00 | >> +------------+--------+ >> >> db=> select a.date, a.amount, b.date, b.amount from a full join b using (date,amount); >> +------------+--------+------------+--------+ >> | date | amount | date | amount | >> +------------+--------+------------+--------+ >> | 2018-01-01 | 10.00 | 2018-01-01 | 10.00 | >> | 2018-02-01 | 5.00 | | | >> | | | 2018-03-01 | 8.00 | >> | 2018-04-01 | 5.00 | 2018-04-01 | 5.00 | >> +------------+--------+------------+--------+ >> >> This works fine until I have multiple items with the same date >> and amount: >> >> db=> select * from a; >> +------------+--------+ >> | date | amount | >> +------------+--------+ >> | 2018-01-01 | 10.00 | >> | 2018-02-01 | 5.00 | >> | 2018-04-01 | 5.00 | >> | 2018-05-01 | 20.00 | <-- >> | 2018-05-01 | 20.00 | <-- >> +------------+--------+ >> >> >> db=> select * from b; >> +------------+--------+ >> | date | amount | >> +------------+--------+ >> | 2018-01-01 | 10.00 | >> | 2018-03-01 | 8.00 | >> | 2018-04-01 | 5.00 | >> | 2018-05-01 | 20.00 | <-- >> | 2018-05-01 | 20.00 | <-- >> +------------+--------+ > > What's your PK on "a" and "b"? These input tables can have duplicate rows, so defining a primary key requires something like a row ID or similar. > (Also, gmail seems to think that all -- or at least most -- of your email is > spam.) Yes, it is becoming increasingly difficult to persuade gmail etc. that you are not a spammer if you run your own mail server. If you have any interesting headers suggesting exactly what they disliked about my message, could you please forward them off-list? Thanks. Regards, Phil.