仅显示重复记录 - Oracle Sql

时间:2014-10-24 00:24:42

标签: sql oracle

构造以下查询以向我提供源等于MLP的所有记录。在某些情况下,每个YPID可能有两个或更多记录。我只需要拉出YPID不止一次显示的记录而排除所有其他记录。如何使用下面的查询完成此操作?

这是我的疑问:

select
mr.ypid,
mr.source_record_id,
mr.business_name,
mr.street_address,
mr.city,
mr.state,
mr.zip5
from MDM2.records mr 
join redirects r on r.source_record_id = mr.source_record_id
where mr.source_code = 'MLP'
and mr.status = 'A'
and mr.listing_advertiser_ind = 'A'
and r.is_ad = '1'
and r.source_customer_id is not null
and r.deleted = '0'
order by mr.ypid;

2 个答案:

答案 0 :(得分:1)

您可以使用cte获取重复记录,并进一步加入查询以获取这些值的数据

with cte
as
(
SELECT mr.ypid
FROM MDM2.records mr
where mr.source_code = 'MLP'
and mr.status = 'A'
and mr.listing_advertiser_ind = 'A'
group by mr.ypid
having count(*) >1
)
select
mr.ypid,
mr.source_record_id,
mr.business_name,
mr.street_address,
mr.city,
mr.state,
mr.zip5
from MDM2.records mr 
join cte on 
mr.ypid = cte.ypid
join redirects r on r.source_record_id = mr.source_record_id
where  r.is_ad = '1'
and r.source_customer_id is not null
and r.deleted = '0'
order by mr.ypid;

答案 1 :(得分:1)

试试这个     选择     mr.ypid,     mr.source_record_id,     mr.business_name,     mr.street_address,     mr.city,     mr.state,     mr.zip5     来自MDM2.records先生,          重定向r,          (选择mr2.ypid,count()cnt           来自MDM2.records mr2,           重定向r2           其中r2.source_record_id = mr2.source_record_id           和mr2.source_code =' MLP'           和mr2.status =' A'           和mr2.listing_advertiser_ind =' A'           和r2.is_ad =' 1'           和r2.source_customer_id不为空           和r2.deleted =' 0'           由mr2.ypid组成           有计数()> 1)作为重复     其中r.source_record_id = mr.source_record_id     和mr.source_code =' MLP'     和mr.status =' A'     和mr.listing_advertiser_ind =' A'     和r.is_ad =' 1'     并且r.source_customer_id不为空     和r.deleted =' 0'     和dups.ypid = mr.ypid     按mr.ypid排序;

注意:我改变了连接方式,使其看起来更清晰。 我们的想法是制作一个虚拟的桌子" dups"只有重复的ypid