pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Phil Endecott <spam_from_pgsql_lists@chezphil.org>
To: John W Higgins <wishdev@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Limitting full join to one match
Date: Thu, 06 Dec 2018 15:43:12 +0000
Message-ID: <1544110992364@dmwebmail.dmwebmail.chezphil.org> (raw)
In-Reply-To: <CAPhAwGw2n4ZNRL6DNaYV0wgRiidp6LW-dj_GVAxaCJK_JB2Q6A@mail.gmail.com>
References: <CAPhAwGw2n4ZNRL6DNaYV0wgRiidp6LW-dj_GVAxaCJK_JB2Q6A@mail.gmail.com>

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.











view thread (7+ messages)  latest in thread

Message-ID: <1544110992364@dmwebmail.dmwebmail.chezphil.org>
Permalink:  ../1544110992364@dmwebmail.dmwebmail.chezphil.org/
Also on:    postgresql.org/message-id/1544110992364@dmwebmail.dmwebmail.chezphil.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: spam_from_pgsql_lists@chezphil.org, wishdev@gmail.com
  Subject: Re: Limitting full join to one match
  In-Reply-To: <1544110992364@dmwebmail.dmwebmail.chezphil.org>
  References: <CAPhAwGw2n4ZNRL6DNaYV0wgRiidp6LW-dj_GVAxaCJK_JB2Q6A@mail.gmail.com> <1544110992364@dmwebmail.dmwebmail.chezphil.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox