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 1gUnSQ-00071r-0b for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 06:48:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUnSN-0005A4-1V for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 06:48:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUnSM-00053s-Kz for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 06:48:18 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUnSI-0006D3-Ng for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 06:48:16 +0000 Received: by mail-ot1-x336.google.com with SMTP id 40so21072996oth.4 for ; Wed, 05 Dec 2018 22:48:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=lEafa3FROgxEhr698xl2thHenQCqpWgiRGYg2itIFws=; b=p8zl0a0rATVnNI56kqa0yKCNjWjEYVfqBwin28CdfaE9dSbNglw22U6pE5B3a9Ehvp caAV70GRlupiDjIL4F7vGHm+dkZ5qZktn5XZe3m74iBM7Ga1rmgp2/5MjYQ8vuxAfmvX 6kKaBzjL8zYh18c7X5o0wvd0vRh023QSL6O10AVPxU/gUg68qWETqwtMDRpZayYxCCiT +7sptCtsqXKje7zODJ64MOjdQ4UukY8w3CGHWQ18HwiJPXUbv6bHKFaIV1eQ2/tN8Oby tbIbsnTvjMivbmCXyThk59pQ5Gy/7KAjMt7J/urLUNF2x84OaIUoHQT/ShVeqgagNkUR LbLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=lEafa3FROgxEhr698xl2thHenQCqpWgiRGYg2itIFws=; b=gUz6LbdyLAn0ZOf+vsD6ikscSs5EXZUDtAJNJEukR2D4lCrc+yuAw08u2YZ4B1uUJU m5hPC8h5Jewj4ncv/W9es4g6I9JNiRoxyuQqh+e2cK6Y/du5p0ROtLp7JHaozjuchvbz /rpBizMQGNCvdDU3NPR8FqynawswUCzuyB/KUQTZSoyptRg8kp5dVJNb/ZysPoK70Vsn gaXzZSDBGbFXiVVd0cPNcC3H70iuM54SnLIhXgK8XxvAye4ePjsCn8sXaKzbcaxJixPk hOtAO78WfHXV6ed1pA4V5Uz/NME3/APMdg094RIRAkFWeHRQZzTbkdng7+/S7l3JBAiF Fj+A== X-Gm-Message-State: AA+aEWbYUeN2wJxesFb0ggN3SwDNl4lQQFFF2rpSWdm1sYIuY0AQ69fD msgknehcJRaJQbduM2QaHh/5U2b6 X-Google-Smtp-Source: AFSGD/VaqwmDYOq5WqeSUTxW1YTCmP1Iatc2hYanS9CxeEv5XExjhCgGw1qQOmmwvnfZLfMbybLqww== X-Received: by 2002:a9d:88d:: with SMTP id 13mr16066882otf.269.1544078891672; Wed, 05 Dec 2018 22:48:11 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id y20sm10386565otk.66.2018.12.05.22.48.10 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Dec 2018 22:48:10 -0800 (PST) Subject: Re: Limitting full join to one match To: pgsql-general@lists.postgresql.org References: <1544056443924@dmwebmail.dmwebmail.chezphil.org> From: Ron Message-ID: <5d194157-789b-6e3c-4827-d462f58f0304@gmail.com> Date: Thu, 6 Dec 2018 00:48:09 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <1544056443924@dmwebmail.dmwebmail.chezphil.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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"? (Also, gmail seems to think that all -- or at least most -- of your email is spam.) > 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 | > | 2018-05-01 | 20.00 | 2018-05-01 | 20.00 | 1 > | 2018-05-01 | 20.00 | 2018-05-01 | 20.00 | 2 > | 2018-05-01 | 20.00 | 2018-05-01 | 20.00 | 3 > | 2018-05-01 | 20.00 | 2018-05-01 | 20.00 | 4 > +------------+--------+------------+--------+ > > It has, of course, put four rows in the output for the new items. > > So my question is: how can I modify my query to output only two rows, > like this:? > > +------------+--------+------------+--------+ > | 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 | > | 2018-05-01 | 20.00 | 2018-05-01 | 20.00 | 1 > | 2018-05-01 | 20.00 | 2018-05-01 | 20.00 | 2 > +------------+--------+------------+--------+ > > > Any suggestions anyone? > > > The best I have found so far is something involving EXCEPT ALL: > > db=> select * from a except all select * from b; > db=> select * from b except all select * from a; > > That's not ideal, though, as what I ultimately want is something > that lists everything with its status: > > +------------+--------+--------+ > | date | amount | status | > +------------+--------+--------+ > | 2018-01-01 | 10.00 | OK | > | 2018-02-01 | 5.00 | a_only | > | 2018-03-01 | 8.00 | b_only | > | 2018-04-01 | 5.00 | OK | > | 2018-05-01 | 20.00 | OK | > | 2018-05-01 | 20.00 | OK | > +------------+--------+--------+ > > That would be easy enough to achieve from the JOIN. > > > Thanks, Phil. > > > -- Angular momentum makes the world go 'round.