Re: Finding missing records - Mailing list pgsql-general

From John D. Burger
Subject Re: Finding missing records
Date
Msg-id 76d00d2736badf0d1593d30d9666d7fb@mitre.org
Whole thread Raw
In response to Finding missing records  ("Stefano B." <stefano.bonnin@comai.to>)
Responses Re: Finding missing records
List 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

pgsql-general by date:

Previous
From: Pandurangan R S
Date:
Subject: Re: Finding missing records
Next
From: Richard Huxton
Date:
Subject: Re: PG_RESTORE and database size