从表中提取记录

时间:2013-10-18 20:57:54

标签: sql postgresql

我想显示一个不在另一个数据集中的数据集的记录。

以下是较大数据集的代码:

select distinct facil_identifier, facil_addr_line1,
trim(facil_city) as facil_city, facil_zip
from xx.tb1 f
where facil_identifier is not null

我想在第一个(较大的)数据集中找到不在较小数据集中的记录。

以下是较小数据集的代码:

select distinct facil_identifier, facil_addr_line1,
facil_zip
from xx.tb1 f
where facil_identifier is not null

这是我的尝试:

select distinct facil_identifier, facil_addr_line1,
facil_zip
from xx.tb1 f
where facil_identifier is not null
and facil_identifier not in
(select facil_identifier
from 
    (select distinct facil_identifier, facil_addr_line1,
    trim(facil_city) as facil_city, facil_zip
    from xx.tb1 f2
    where facil_identifier is not null) as city
where city.facil_identifier = f.facil_identifier)
order by facil_addr_line1

这段代码费用很高。每组记录少于3000条。

1 个答案:

答案 0 :(得分:0)

我不是postgres SQL的专家,但是应该这样做(或者至少让你接近)

select distinct f2.facil_identifier, f2.facil_addr_line1,
trim(f2.facil_city) as facil_city, f2.facil_zip
from import.facil_xx_20131016 f2
left join import.facil_xx_20131016 f on f2.facil_identifier = f.facil_identifier
where f2.facil_identifier is not null
and f.facil_identifier is null