The total Numbers of record in table gym_membercommon are 40352.
The total numbers of records for tenant 3 is 10250.
In the table gym_membercommon i need to find all the duplicate records that have any of the number common within that tenant.
create table #temp
(
meco_Commonid int,
meco_tenantid int,
meco_OfficeTelno varchar(30),
meco_HomeNo varchar(20),
meco_MobileNo varchar(20),
meco_Fax varchar(20)
)
CREATE CLUSTERED INDEX idxCL_TEMP ON #temp(meco_Commonid)
CREATE NONCLUSTERED INDEX idxNC_TEMP ON #temp(meco_OfficeTelno,meco_HomeNo,meco_MobileNo,meco_Fax)
insert into #temp
select
meco_Commonid,
meco_tenantid,
meco_OfficeTelno,
meco_HomeNo,
meco_MobileNo,
meco_Fax
from gym_membercommon a
where
meco_tenantId = 1
And
lower(ltrim(rtrim(meco_status))) <> 'erroneous'
Select distinct a.*
from #temp a
inner join #temp b
on
(
(ltrim(rtrim(isnull(a.meco_officeTelno,''))) <>'' and a.meco_officeTelno in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or
(ltrim(rtrim(isnull(a.meco_HomeNo,''))) <>'' and a.meco_HomeNo in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or
(ltrim(rtrim(isnull(a.meco_MobileNo,''))) <>'' and a.meco_MobileNo in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax)) or
(ltrim(rtrim(isnull(a.meco_Fax,''))) <>'' and a.meco_Fax in (b.meco_OfficeTelno,b.meco_HomeNo,b.meco_MobileNo,b.meco_Fax))
)
and a.meco_Commonid <> b.meco_commonid
And a.meco_tenantId = 1
Awaiting for your reply
thanks in advance.
Dasharath Yadav
Fitness Force