Re: Finding records that are not there

From: "Adam Rich" <adam(dot)r(at)sbcglobal(dot)net>
To: "'Owen Hartnett'" <owen(at)clipboardinc(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Finding records that are not there
Date: 2008-05-22 16:46:52
Message-ID: 05e901c8bc2b$707fd2e0$517f78a0$@r@sbcglobal.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> I have two tables that have identical index fields, maplot and
> unitno, (both indexes span two columns) and I want to find all the
> records in the commcost table that don't have a corresponding record
> in the bldg file.
>
> The SQL I've tried is:
>
> select commcost.maplot, commcost.unitno from commcost
> where not exists(select 1 from commcost, bldg
> where commcost.maplot = bldg.maplot and
> commcost.unitno = bldg.unitno)
> order by commcost.maplot
>
> It returns no records although I know that there are records in
> commcost which do not match keys with records from bldg.
>

You shouldn't put "commcost" in your inner select, since it's
already in your outer select.

Or try this, it's probably faster:

Select commcost.maplot, commcost.unitno from commcost c
left join bldg b on c.maplot = b.maplot and c.unitno = b.unitno
where b.unitno is null

In response to

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2008-05-22 16:47:33 Re: Finding records that are not there
Previous Message Owen Hartnett 2008-05-22 16:21:35 Finding records that are not there