Re: Finding missing records

From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: "Stefano B(dot)" <stefano(dot)bonnin(at)comai(dot)to>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Finding missing records
Date: 2006-01-27 14:09:34
Message-ID: 76d00d2736badf0d1593d30d9666d7fb@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Jan 27, 2006, at 08:59, Stefano B. wrote:

> select f1,f2,f3,f4 from table1 where (f1,f2,f3,f4) NOT IN (select
> f1,f2,f3,f4 from table2)
>  
> but it seems not work (as I want). It returns me no records. If I use
> the IN clause it returns me all 10000 table1 records.

The standard way to do this is:

select f1,f2,f3,f4 from table1
except
select f1,f2,f3,f4 from table2;

Note that IN and EXCEPT are essentially set operators - if you have
duplicates in either table, you might not get what you expect. Your
last comment above seems to indicate that this is indeed the case.

If what you want is the =bag= difference of the two tables, you'll have
to do something more complicated. Possible solutions might involve
counting duplicates in both tables with a COUNT(*) and GROUP BY, and
then joining on the four columns and subtracting the counts.

- John D. Burger
MITRE

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2006-01-27 14:09:45 Re: PG_RESTORE and database size
Previous Message Pandurangan R S 2006-01-27 14:03:59 Re: Finding missing records