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 1gUhcN-0007rE-3Z for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 00:34:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUhcK-0004BT-Um for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 00:34:12 +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 1gUhcK-0004BM-Jp for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 00:34:12 +0000 Received: from ec2-34-255-138-49.eu-west-1.compute.amazonaws.com ([34.255.138.49] helo=zimbabwe.chezphil.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUhcD-0006Nk-JW for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 00:34:11 +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: Message-ID:Subject:Date:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=IpjoOiEr7r/3hanbqCdY/vAKNKqsFqrw7pAgBciUmsw=; b=vPkziTiPdlBVx5xOz7wiXTc91 EKHZkL9OG2fvZMu7x3GvT3FdfSN4F/6B/HU4c2eAghbREg2486r2lU0x6YLmBXJQ3DM/bLy8hxSTK Bl6iJtel2eQyH+cIuzK/LAtaS3iGQx0q3nHC4jmuhaE21+BNn7RqwOGHZqX8wSr4fNkso=; Received: from localhost ([127.0.0.1] helo=chezphil.org) by zimbabwe.chezphil.org with esmtp (Exim 4.89) (envelope-from ) id 1gUhcC-0001mM-4o for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 00:34:04 +0000 Received: from localhost ([::ffff:127.0.0.1]) by zimbabwe with smtp (dmsmtpd 0.00001); Thu, 06 Dec 2018 00:34:04 +0000 From: "Phil Endecott" To: Date: Thu, 06 Dec 2018 00:34:03 +0000 Subject: Limitting full join to one match Message-ID: <1544056443924@dmwebmail.dmwebmail.chezphil.org> X-Mailer: Decimail Webmail 3alpha16 MIME-Version: 1.0 Content-Type: text/plain; format="fixed"; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 | <-- +------------+--------+ 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.