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 1gUmjH-0005HX-J2 for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Dec 2018 06:01: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 1gUmjF-0002Ap-Qe for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Dec 2018 06:01:41 +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 1gUmjF-0002Ai-Ef for pgsql-hackers@lists.postgresql.org; Thu, 06 Dec 2018 06:01:41 +0000 Received: from mail-pf1-x443.google.com ([2607:f8b0:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUmj7-000579-7p for pgsql-hackers@postgresql.org; Thu, 06 Dec 2018 06:01:40 +0000 Received: by mail-pf1-x443.google.com with SMTP id y126so11253073pfb.4 for ; Wed, 05 Dec 2018 22:01:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=UUyQ51ObHx1IjWkig7ammbx3WcPnwxCGQQVqRS6zcdA=; b=RwsYlS813r+MDnMyQZ5Z3D4YYgAR1I6JdAC8kwUYUJELVsh9RYYXmiWQsB6fyBkeUr ccm1/dpfkC6UDHUnXmOh008XsS8zH1Twt0sqLcnJn0GlNllYqOq0hFDSNNTO8zRoinyE WnqjcB3BmEjAQwNqtROP/k8q1zGWr4OxzdTxe9Usun4rRAwgaP8Ne2IOCNBI2Gs7/J52 KPkmKx4TUcT2vCcsCv3aDmYAIKPrAgFRjDM+hmZRG2oUoe8ZnDtHaChgRBvLDnjPCOid gV/hiIY7p5zzcRXSkmdfPL4Cc1enXyZ3ZaveglDVJpI+fkoaAQIx2WXW/B42zmAStV4l 0yxA== 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:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=UUyQ51ObHx1IjWkig7ammbx3WcPnwxCGQQVqRS6zcdA=; b=e3UT4l/CKhwj3/NM0G42vVwUIKl4MAYYl2XNvJAKkRDM7s572yhmoPx4lpHwNp1Pgy z51aWdz3HN7y9mWX9kUkgupqlQu76X1iCT6DrXQsIAZrrP3OZkrJdkmC0y7crWd7OapL 4SuOjX4NupaB7fZmJ8DtKrr0LcR0HMpU0m39zuR3Fb4w0VSknxqt/4zJVVqQwJPxixq5 tZs3rjwVPxNy5MN86SnVyDSN9GjzEfrgeWIVApQNYgyv4ILRkxOceUcF5gQ/xLdUgdtg rnHyqRgBvM/8PfrsnEFy4V4bJaoSHVkBWum/AK1zzYLJCp/zqaiT7DOU+mKrKrsW8ujC EkGQ== X-Gm-Message-State: AA+aEWbiLs13SPvV2bXwluWswUC0fPq5dk5nhK/bt9AJbAs4MPoZyQNI S+g+JW1I7FP2k3wjAtnqHWVAetSb X-Google-Smtp-Source: AFSGD/Ur1zyTnCiAAGjOnJf2/4BF8X+WflSWsN0LE6fdgKzK8WC++CRYDEiizhBRauvQUh0iHkcpAA== X-Received: by 2002:a65:4049:: with SMTP id h9mr22716568pgp.304.1544076089992; Wed, 05 Dec 2018 22:01:29 -0800 (PST) Received: from ?IPv6:2001:56a:703e:dc00:ac4e:5594:a490:c626? (node-1w7jr9y4y5kkq39l3jnr5o2zq.ipv6.telus.net. [2001:56a:703e:dc00:ac4e:5594:a490:c626]) by smtp.gmail.com with ESMTPSA id l5sm21222897pgp.82.2018.12.05.22.01.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Dec 2018 22:01:29 -0800 (PST) Subject: Re: Limitting full join to one match To: John W Higgins References: <1544056443924@dmwebmail.dmwebmail.chezphil.org> Cc: pgsql-hackers@postgresql.org From: Sergei Agalakov Message-ID: <0a683148-5330-bd53-c3dc-7fb953e73874@gmail.com> Date: Wed, 5 Dec 2018 23:01:27 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.3.2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------C757879B373BD9D633F30E95" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------C757879B373BD9D633F30E95 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 12/5/2018 8:30 PM, John W Higgins wrote: > > > On Wed, Dec 5, 2018 at 4: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: > > ... > > 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); > > Example here - http://sqlfiddle.com/#!17/305d6/3 > > John > > > 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. > This question is always asked time to time. I have found an old article with so far the best solution for big tables. https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:2151582681236#15393095283923 On the same test data create table a (date date, amount money); create table b (date date, amount money); insert into a values ('2018-01-01', 10); insert into a values ('2018-02-01', 5); insert into a values ('2018-04-01', 5); insert into a values ('2018-05-01', 20); insert into a values ('2018-05-01', 20); insert into b values ('2018-01-01', 10); insert into b values ('2018-03-01', 8); insert into b values ('2018-04-01', 5); insert into b values ('2018-05-01', 20); insert into b values ('2018-05-01', 20); select tt.date,        tt.amount,        count(tt.src1) CNT1,        count(tt.src2) CNT2  from (  select a.date,         a.amount,         1 src1,         null::integer src2    from a  union all  select b.date,         b.amount,         null::integer src1,         2 src2   from b   ) tt group by tt.date, tt.amount; date          amount    cnt1    cnt2 2018-01-01    $10.00     1       1 2018-02-01    $5.00      1       0 2018-03-01    $8.00      0       1 2018-04-01    $5.00      1       1 2018-05-01    $20.00     2       2 It requires a sort, so you may want to increase work_mem before execution, and then return it back like SET work_mem = '512MB'; ...  run your query RESET work_mem; Regards, Sergei Agalakov --------------C757879B373BD9D633F30E95 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 12/5/2018 8:30 PM, 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);


John 

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.


This question is always asked time to time.
I have found an old article with so far the best solution for big tables.
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:2151582681236#15393095283923

On the same test data
create table a (date date, amount money);
create table b (date date, amount money);

insert into a values ('2018-01-01', 10);
insert into a values ('2018-02-01', 5);
insert into a values ('2018-04-01', 5);
insert into a values ('2018-05-01', 20);
insert into a values ('2018-05-01', 20);
insert into b values ('2018-01-01', 10);
insert into b values ('2018-03-01', 8);
insert into b values ('2018-04-01', 5);
insert into b values ('2018-05-01', 20);
insert into b values ('2018-05-01', 20);

select tt.date,
       tt.amount,
       count(tt.src1) CNT1,
       count(tt.src2) CNT2
 from
(
 select a.date,
        a.amount,
        1 src1,
        null::integer src2
   from a
 union all
 select b.date,
        b.amount, 
        null::integer src1,
        2 src2
  from b
  ) tt
group by tt.date, tt.amount;

date          amount    cnt1    cnt2
2018-01-01    $10.00     1       1
2018-02-01    $5.00      1       0
2018-03-01    $8.00      0       1
2018-04-01    $5.00      1       1
2018-05-01    $20.00     2       2

It requires a sort, so you may want to increase work_mem before execution, and then return it back like
SET work_mem = '512MB';
...  run your query
RESET work_mem;

Regards,

Sergei Agalakov
--------------C757879B373BD9D633F30E95--