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 1gUvqp-00059d-4f for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 15:46:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUvqn-0003TO-Q7 for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 15:46:05 +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 1gUvqF-0002zj-58 for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 15:45:31 +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 1gUvq8-0005js-CU for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 15:45:29 +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=ezj8HU2k9PvRMb1xt5TuVrxVR7X4/ESBKcdQvKWC9oE=; b=sG1UZtJho5wYKEVr8f+RRouBn 33g64dm7hGFZht5Bmq+5PtVhaW3p9nmfb9RbMvgwqO7dQpFu+dl+TAkLGmiKjiNHq4oTRbwrlfe1o lyv/9Kvt6/nII5MjusRjPbXleNWz0vY8ZRfdEinmRKYfSARZGhkX6eJip+gkcrzBAT0Tw=; Received: from localhost ([127.0.0.1] helo=chezphil.org) by zimbabwe.chezphil.org with esmtp (Exim 4.89) (envelope-from ) id 1gUvo0-0004Qe-Hc for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 15:43:12 +0000 Received: from localhost ([::ffff:127.0.0.1]) by zimbabwe with smtp (dmsmtpd 0.00001); Thu, 06 Dec 2018 15:43:12 +0000 From: "Phil Endecott" To: "John W Higgins" Cc: Date: Thu, 06 Dec 2018 15:43:12 +0000 Subject: Re: Limitting full join to one match Message-ID: <1544110992364@dmwebmail.dmwebmail.chezphil.org> In-Reply-To: References: 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 John W Higgins wrote: > On Wed, Dec 5, 2018 at 4:34 PM Phil Endecott < > spam_from_pgsql_lists@chezphil.org> 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: >> >> ... > > >> 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 >> +------------+--------+------------+--------+ >> >> > Evening Phil, > > Window functions are your friend here. I prefer views for this stuff - but > subqueries would work just fine. > > create view a_rows as (select *, > row_number() OVER (PARTITION BY date, amount) AS pos > from a); > create view b_rows as (select *, > row_number() OVER (PARTITION BY date, amount) AS pos > from b); > > select > a_rows.date, > a_rows.amount, > a_rows.pos, > b_rows.date, > b_rows.amount, > b_rows.pos > from > a_rows full join b_rows using (date,amount,pos); Thanks John, that's great. I'm a little surprised that there isn't an easier way, but this certainly works. Regard, Phil.