Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Case Statements to Match Fields

Author  Topic 

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-21 : 13:14:51
I have been working on a code since last week and I have tried so many routes but all of them have partially or fully failed. This is what I am trying to accomplish. I need to take a set of data which has several fields like name, account number, city , state, address, bill address, group code, registration number, phone number. Once I have get this information than I have to try and match this group of data with the main data to see where one of these fields match but not based on account number, I need to match them with the other fields and than write a case statment and divide them in the group of 100%, 80%, 60%, 40%, 20% , 10% and 0%. If all the fields match like name, city , state, address, bill address, group code, registration number, phone number than 100% and so on and so forth. Here is what I am trying to now but this seems to be not workign either can anyone please help. here is my code.


--this is the code where I match them with the main data---
select
a.accnt_no as TmpAccnt_no, b.accnt_no as IBS_Accnt_no,
a.BillAddress1,d.accnt_ln1_ad as Bill_To_Address,
a.Physical_Address_1, b.accnt_ln1_ad as IBS_Phys_Address,
a.city,
b.accnt_city_ad,
a.accnt_nm,
b.accnt_nm as IBS_Accnt_Nm,
a.major_accnt_cd, b.major_accnt_cd as IBS_Mac_Code,
a.Accnt_grp as Tmp1Accnt_grp, b.Accnt_grp,
a.[ibs dud], c.Taxpayer_Registration_Code,
a.cntct_ph_no as Contact_ph, e.cntct_ph_no
into #tmp1
from #tmp a
inner join daily_prod.dbo.IBS_CUST_MASTER_DND b
on a.Physical_Address_1 = b.accnt_ln1_ad
inner join daily_prod.dbo.IBS_CUST_TAX_PYR_REGIS_DND c
on a.[ibs dud] = c.Taxpayer_Registration_Code
inner join daily_prod.dbo.IBS_ACCT_BILLTO_ADDRESS_DND d
on a.billaddress1 = d.accnt_ln1_ad
inner join daily_prod.dbo.IBS_CUST_MASTER_EXTRA_DATA_DND e
on a.cntct_ph_no = e.cntct_ph_no
where
a.accnt_no <> b.accnt_no
and a.accnt_no <> c.accnt_no
and a.accnt_no <> d.accnt_no
and a.accnt_no <> e.accnt_no
and a.billaddress1 <> '' and a.billaddress1 <> 'ACCOUNTS PAYABLE'
and d.accnt_ln1_ad <> '' and d.accnt_ln1_ad <> 'ACCOUNTS PAYABLE'
and a.Physical_Address_1 <> '' and a.Physical_Address_1 <> 'ACCOUNTS PAYABLE'
and b.accnt_ln1_ad <> '' and b.accnt_ln1_ad <> 'ACCOUNTS PAYABLE'
and a.major_accnt_cd <> '' and b.major_accnt_cd <> ''
and a.[ibs dud] <> '' and c.Taxpayer_Registration_Code <> ''


--than this is where I wrote my case

select distinct 'confidence' = case
when physical_Address_1 = IBS_Phys_Address
and BillAddress1 <> Bill_To_Address
and city <> accnt_city_ad
and Accnt_nm <> IBS_Accnt_Nm
and Major_accnt_cd <> ibs_mac_code
and [ibs dud] <> Taxpayer_Registration_Code
and cntct_ph_no <> Contact_ph
then '10%'
when physical_Address_1 = IBS_Phys_Address
and BillAddress1 = Bill_To_Address
and city <> accnt_city_ad
and Major_accnt_cd <> ibs_mac_code
and [ibs dud] <> Taxpayer_Registration_Code
and cntct_ph_no <> Contact_ph
and Accnt_nm <> IBS_Accnt_Nm
then '20%'
when physical_Address_1 = IBS_Phys_Address
and BillAddress1 = Bill_To_Address
and city = accnt_city_ad
and [ibs dud] <> Taxpayer_Registration_Code
and cntct_ph_no <> Contact_ph
and Major_accnt_cd <> ibs_mac_code
and Accnt_nm <> IBS_Accnt_Nm
then '40%'
when physical_Address_1 = IBS_Phys_Address
and BillAddress1 = Bill_To_Address
and [ibs dud] = Taxpayer_Registration_Code
and cntct_ph_no = Contact_ph
and Major_accnt_cd <> ibs_mac_code
and Accnt_nm <> IBS_Accnt_Nm
and city <> accnt_city_ad
then '60%'
when physical_Address_1 = IBS_Phys_Address
and BillAddress1 = Bill_To_Address
and cntct_ph_no = Contact_ph
and Major_accnt_cd = ibs_mac_code
and Accnt_nm = IBS_Accnt_Nm
and [ibs dud] <> Taxpayer_Registration_Code
and city <> accnt_city_ad
then '80%'
when physical_Address_1 = IBS_Phys_Address
and cntct_ph_no = Contact_ph
and Major_accnt_cd = ibs_mac_code
and Accnt_nm = IBS_Accnt_Nm
and [ibs dud]= Taxpayer_Registration_Code
and city = accnt_city_ad
and BillAddress1 = Bill_To_Address
then '100%'
--when physical_Address_1 = IBS_Phys_Address
--and Major_accnt_cd <> ibs_mac_code
--and Accnt_nm <> IBS_Accnt_Nm
--and [ibs dud] <> Taxpayer_Registration_Code
--and city <> accnt_city_ad
--and BillAddress1 <> Bill_To_Address
--and cntct_ph_no <> Contact_ph
--then '0%'
else '0%' end,
IBS_Accnt_no,
physical_Address_1, IBS_Phys_Address, BillAddress1, Bill_To_Address,
city, accnt_city_ad,
Accnt_nm, IBS_Accnt_Nm,
Major_accnt_cd, ibs_mac_code,
[ibs dud], Taxpayer_Registration_Code,
cntct_ph_no, Contact_ph
from #tmp1

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-21 : 21:33:55
Ahmed, I must admit that I did not read the code you posted, it's too much information for my head to hold. But I read VERY carefully your first paragraph where you have outlined your requirement. Tell me if what you want is something like this:
-- create the MAIN DATA TABLE and fill it with some junk.
create table MainData( id int, f1 varchar(255), f2 varchar(255), f3 varchar(255), f4 varchar(255));

insert into MainData values (1, 'Sunita', 'Beck', 'Stamford', 'CT');
insert into MainData values (2, 'Jaya','Gopal', 'Greenwich', 'CT');
insert into MainData values (3, 'Josh','Murry', 'New York', 'NY');

-- create the TEST DATA TABLE and fill it with some other junk.
create table NewData(id int, f1 varchar(255), f2 varchar(255), f3 varchar(255), f4 varchar(255));
insert into NewData values (1, 'Sunita', 'Smith', 'New York', 'NY');
insert into NewData values (2, 'Jaya','Gopal', 'Greenwich', 'CT');
insert into NewData values (3, 'Josh','Smith', 'Hartford', 'CT');

-- see the raw junk.
select * from MainData;
select * from NewData;

-- Do the field matching calculation.
with cte as
(
select
case when n.f1 = m.f1 then 1 else 0 end +
case when n.f2 = m.f2 then 1 else 0 end +
case when n.f3 = m.f3 then 1 else 0 end +
case when n.f4 = m.f4 then 1 else 0 end as MatchingFieldCount,
n.*
from
NewDAta n cross join MainDAta m
)
select
max(MatchingFieldCount) as BestMatchFieldCount,
max(MatchingFieldCount)*100/4.0 as BestMatchPercent, -- dividing by 4 because we are matching on 4 fields.
id,f1,f2,f3,f4
from
cte
group by
id,f1,f2,f3,f4
order by
id;

-- clean up the junk.
drop table MainData;
drop table NewData;
Given my sample input data, if the output this produces is not what you are looking for, tell me what the output should look like.
Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-22 : 09:14:24
yes this is what I want. But how do I achieve that with my data.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-22 : 09:34:35
I was only showing the concept of how I would do it Ahmed. You would need to take that concept and apply it to your data.

If I understood you correctly, you have two tables in your database - the "Main Table" and the "New Data" table. From my example, take the section of code that starts with "with cte as", and replace the two table names in that query with your actual table names. Then, replace Id, f1, f2, f3 etc. with the actual column names in your tables.

Id in my example would be equivalent of "Account Number" in your data, which you want to exclude from comparison. Other columns such as name, city, state, address, bill address, etc. in your data, which you do want to compare, would be equivalent to f1, f2, f3 etc. in my example.
Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-22 : 09:45:15
one more question. What if I need to look for the fields in multiple tables. Can cross join be used for multiple tables?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-22 : 10:11:41
You don't want to join multiple tables. Instead, what you should do is to write a query that will select everything you want for the main tables by doing inner joins (or outer joins as the case may be) and a similar one for the new data tables and THEN cross join the two. For example, let us say your "main data" came from two tables and the new data came from three tables. Then instead of
NewDAta n cross join MainDAta m  
you would do something like this:
(	
NewData1 n1
inner join NewData2 n2 on n2.acct_cd = n1.acct_cd -- I am making it up. Instead of acct_cd use what you need to join on.
inner join NewData3 n3 on n3.order_id = n1.order_id -- Again, making it up.
)
cross join
(
Maindata1 m1
inner join MainData2 m2 on m2.trade_Id = m1.trade_id
)
Of course, then you will need to refer to n1, n2 etc. for column names in the select part of your query.
Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-22 : 10:31:41
but the case statment part will remain the same correct?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-22 : 11:30:40
Ahmed, yes to your question with one thing to note. You would need to refer to the correct table where your data is located. For example, if the LastName of a customer is in NewData1 (which we aliased as n1) and City is in in NewData2 (which we aliased as n2), then you will need to use n1.LastName for the LastName and n2.City for the City.
Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-22 : 12:05:17
okay I wrote this portion. but I am having difficulty doing the cross join. Can you please help. I would appreciate it.

with cte as
(
select
case when n.Physical_address_1 = m.accnt_ln1_ad then 1 else 0 end +
case when n.accnt_nm = m.accnt_nm then 1 else 0 end +
case when n.City = m.Accnt_city_ad then 1 else 0 end +
case when n.Major_accnt_cd = m.Major_accnt_cd then 1 else 0 end +
case when n.[IBS DUD] = o.Taxpayer_Registration_Code then 1 else 0 end +
case when n.cntct_ph_no = p.cntct_ph_no then 1 else 0 end +
case when n.billaddress1 = q.accnt_ln1_ad then 1 else 0 end as MatchingFieldCount,
n.accnt_no as TmpAccnt, m.accnt_no as IBS_Accnt, n.Physical_address_1 as Phys_address,
m.accnt_ln1_ad as IBS_Phy_Address,
n.Billaddress1 as BillAddress1, q.accnt_ln1_ad as IBS_Bill_address,
n.City as TmpCity, m.Accnt_city_ad as ibs_City, n.accnt_nm as Tmp_Name, m.accnt_nm as IBS_Name,
n.Major_accnt_cd as tmpMAC_Code, m.Major_accnt_cd,
n.[IBS DUD] as ibs_DUD, o.Taxpayer_Registration_Code, n.cntct_ph_no tmp_Ph, p.cntct_ph_no
from
#tmp n
inner join daily_prod.dbo.IBS_CUST_MASTER_DND m on n.Physical_address_1 = m.accnt_ln1_ad and n.accnt_nm = m.accnt_nm and n.city = m.accnt_city_ad and n.major_accnt_cd = m.major_accnt_cd
inner join daily_prod.dbo.IBS_CUST_TAX_PYR_REGIS_DND o on n.[ibs dud] = o.Taxpayer_Registration_Code
inner join daily_prod.dbo.IBS_CUST_MASTER_EXTRA_DATA_DND p on n.cntct_ph_no = p.cntct_ph_no
inner join daily_prod.dbo.IBS_ACCT_BILLTO_ADDRESS_DND q on n.billaddress1 = q.accnt_ln1_ad
where n.accnt_no <> m.accnt_no
)
select
max(MatchingFieldCount) as BestMatchFieldCount,
max(MatchingFieldCount)*100/7.0 as BestMatchPercent, -- dividing by 4 because we are matching on 4 fields.
TmpAccnt, IBS_Accnt, Phys_address, IBS_Phy_Address, BillAddress1, IBS_Bill_address,
TmpCity, ibs_City, Tmp_Name, IBS_Name, tmpMAC_Code, Major_accnt_cd,
ibs_DUD, Taxpayer_Registration_Code, tmp_Ph, cntct_ph_no
from
cte
group by
TmpAccnt, IBS_Accnt, Phys_address, IBS_Phy_Address, BillAddress1, IBS_Bill_address,
TmpCity, ibs_City, Tmp_Name, IBS_Name, tmpMAC_Code, Major_accnt_cd,
ibs_DUD, Taxpayer_Registration_Code, tmp_Ph, cntct_ph_no
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-22 : 12:21:39
I am not going to be of much help here Ahmed. I stared at it for 10 minutes, and really could not make any progress. It is very hard for me to do anything without knowing your table structures and understanding your business rules.

What I would propose is this: Do it one step at a time. For example, first, say to yourself that you are going to match only on 2 fields. May be City and Major_acct_cd. But pick those 2 fields such that they are in one table, for example in IBS_CUST_MASTER_DND, if that is where they are. Then, write the query for that, make it work. That wouldn't need any inner joins - it can be very similar to the example I posted. Once you are able to make that work, THEN, add one more field, and so on.

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-22 : 13:47:09
Just Curious...

What is this suppose to be doing...describe it as you would if you writing a spec

No code or pseudo code...just business language

It seems to me that you are doing way too much...


What the very first thing you need to find



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-22 : 15:07:34
Okay. Here is it in simple language. Three tables have similar types of data. A list of accounts, run it through first table and get basic information like name, city, address, ph, regis_id, group code. Than take that information and run it through the other two tables to see where the account does not match but the field to field match like account 123 has same name, city, address , ph as the account 321 where that type of match occurs based on the number of fields that it matched on apply a percentage level, like if all the fields matched its 100%, 5 match 80% 4 match 60% so on and so forth. Does this makes sense now?
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-03-22 : 15:44:04
One way of doing it could be this

Declare @tableA table (AcctId int,name varchar(20), city varchar(20), [address] varchar(20))
Declare @tableB table (AcctId int,name varchar(20), city varchar(20), [address] varchar(20))
insert into @tableA values (1,'ABC','cityA','AddressA'),(2,'Xyz','cityB','AddressB'),(3,'JKL','cityC','AddressC')
insert into @tableB values (1,'ABC','cityA','AddressA'),(2,'Xyz','cityB','AddressX'),(3,'JKL','cityZ','AddressZ')
select A.AcctId,A.Name,A.city,A.address , (Case When (A.[address]=B.[Address]and A.city=B.city and A.name=B.name) then '100% Match'
When (A.[address]<>B.[Address] and A.city=B.city and A.name=B.name) Then '66% Match'
When (A.[address]<>B.[Address] and A.city<>B.city and A.name=B.name) Then '33% Match' end) as MatchStatus
from @tableA A
inner join @tableB B on A.AcctId=B.AcctId

Cheers
MIK
Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-22 : 17:06:19
nothing works but thank you for everyone's help
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-03-22 : 17:21:25
Here's another approach that may interest you:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=157456
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-22 : 20:43:13
quote:
Originally posted by aahmed1997

nothing works but thank you for everyone's help



Aw...... After all the efforts you put into it, dont give up!

If you break it up into pieces by trying to find matches only on one or two fields to begin with, it would be much easier.

And, if you do it that way, the code will be shorter. So if you do get stuck you can post the code to here, and it will be easier for people to understand and help you fix the problems.
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-03-23 : 03:07:19
perhaps if you try with the ideas we have given to you, maybe you will get what you are looking for ... since the actual data and design is not with us so we are unable to give you the exact query/script. However as per given scenario we have suggested how could you carry it out ...

Cheers
MIK
Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-23 : 16:34:47
okay here I have only one table now that I am doing a cross join. I am pulling all the fields from this table. here is my code.


with cte as
(
select
case when n.Physical_address_1 = m.Phys_add_1 then 1 else 0 end +
case when n.accnt_nm = m.accnt_nm then 1 else 0 end +
case when n.City = m.Phs_city then 1 else 0 end +
case when n.Major_accnt_cd = m.Mac_code then 1 else 0 end +
case when n.[IBS DUD] = m.Taxpayer_Registration_Code then 1 else 0 end +
case when n.cntct_ph_no = m.cntct_ph_no then 1 else 0 end +
case when n.billaddress1 = m.Bill_address1 then 1 else 0 end as MatchingFieldCount,
n.accnt_no as TmpAccnt, m.accnt_no as IBS_Accnt,
n.Physical_address_1 as Phys_address,
m.Phys_add_1 as IBSPhy_Address, n.Billaddress1 as BillAddress1,
m.Bill_address1 as IBS_Bill_address,
n.City as TmpCity, m.Phs_city as Phs_city,
n.accnt_nm as Tmp_Name, m.accnt_nm as IBS_Name,
n.Major_accnt_cd as tmpMAC_Code, m.Mac_code,
n.[IBS DUD] as ibs_DUD, m.Taxpayer_Registration_Code,
n.cntct_ph_no tmp_Ph, m.cntct_ph_no
from
#tmp n cross join #tmp2 m
)
select
max(MatchingFieldCount) as BestMatchFieldCount,
max(MatchingFieldCount)*100/7.0 as BestMatchPercent, -- dividing by 4 because we are matching on 4 fields.
TmpAccnt, IBS_Accnt, Phys_address, IBSPhy_Address,
BillAddress1, IBS_Bill_address,
TmpCity, Phs_city, Tmp_Name, IBS_Name,
tmpMAC_Code, Mac_code
Ibs_DUD, Taxpayer_Registration_Code, tmp_Ph, cntct_ph_no
from
cte
group by TmpAccnt, IBS_Accnt, Phys_address, IBSPhy_Address,
BillAddress1, IBS_Bill_address,
TmpCity, Phs_city, Tmp_Name, IBS_Name,
tmpMAC_Code, Mac_code,
Ibs_DUD, Taxpayer_Registration_Code, tmp_Ph, cntct_ph_no
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-23 : 17:35:06
Ok, here is my attempt to make it work. I am assuming here that #tmp has the table which has the "new data" which you are trying to match up and #tmp2 has the "main data".
with cte as
(
select
case when n.Physical_address_1 = m.Phys_add_1 then 1 else 0 end +
case when n.accnt_nm = m.accnt_nm then 1 else 0 end +
case when n.City = m.Phs_city then 1 else 0 end +
case when n.Major_accnt_cd = m.Mac_code then 1 else 0 end +
case when n.[IBS DUD] = m.Taxpayer_Registration_Code then 1 else 0 end +
case when n.cntct_ph_no = m.cntct_ph_no then 1 else 0 end +
case when n.billaddress1 = m.Bill_address1 then 1 else 0 end as MatchingFieldCount,

-- from here on down, list ONLY columns from the new data table.
n.accnt_no as TmpAccnt,
n.Physical_address_1 as Phys_address,
n.Billaddress1 as BillAddress1,
n.City as TmpCity,
n.accnt_nm as Tmp_Name,
n.Major_accnt_cd as tmpMAC_Code,
n.[IBS DUD] as ibs_DUD,
n.cntct_ph_no tmp_Ph
from
#tmp n cross join #tmp2 m
)
select
max(MatchingFieldCount) as BestMatchFieldCount,
max(MatchingFieldCount)*100/7.0 as BestMatchPercent,
TmpAccnt,
Phys_address,
BillAddress1,
TmpCity,
Tmp_Name,
tmpMAC_Code,
Ibs_DUD,
tmp_Ph
from
cte
group by
TmpAccnt,
Phys_address,
BillAddress1,
TmpCity,
Tmp_Name,
tmpMAC_Code,
Ibs_DUD,
tmp_Ph

This will just tell you what the best match is - it won't yet tell you which specific row in the "main data" it matched against. Once you are able to get this working, we can enhance it to figure that out.
Go to Top of Page

aahmed1997
Starting Member

14 Posts

Posted - 2011-03-24 : 11:46:20
okay its working now. Fabulous. I actually am very excited. Now i have tried to enhance it to include the main data table fields here is my code. Can you tell me if this looks good?

with cte as
(
select
case when n.Physical_address_1 = m.Phys_add_1 then 1 else 0 end +
case when n.accnt_nm = m.accnt_nm then 1 else 0 end +
case when n.City = m.Phys_city then 1 else 0 end +
case when n.Major_accnt_cd = m.mac_code then 1 else 0 end +
case when n.[IBS DUD] = m.Taxpayer_Registration_Code then 1 else 0 end +
case when n.cntct_ph_no = m.contact_ph then 1 else 0 end +
case when n.billaddress1 = m.Bill_add_1 then 1 else 0 end as MatchingFieldCount,
n.accnt_no as TmpAccnt, m.accnt_no as IBS_Accnt,
n.Physical_address_1 as Phys_address,
m.Phys_add_1 as IBS_Phy_Address,
n.Billaddress1 as BillAddress1, m.bill_add_1 as IBS_Bill_address,
n.City as TmpCity, m.BillCity as ibs_City,
n.accnt_nm as Tmp_Name, m.accnt_nm as IBS_Name,
n.Major_accnt_cd as tmpMAC_Code, m.MAC_code,
n.[IBS DUD] as ibs_DUD, m.Taxpayer_Registration_Code,
n.cntct_ph_no as tmp_Ph, m.contact_ph
from
#tmp n cross join #tmp3 m
)
select
max(MatchingFieldCount) as BestMatchFieldCount,
max(MatchingFieldCount)*100/7.0 as BestMatchPercent,
TmpAccnt, IBS_Accnt, Phys_address, IBS_Phy_Address,
BillAddress1, IBS_Bill_address,
TmpCity, ibs_City, Tmp_Name, IBS_Name, tmpMAC_Code, Mac_code,
Ibs_DUD, Taxpayer_Registration_Code, tmp_Ph, contact_ph
into #tmp4
from
cte
group by TmpAccnt, IBS_Accnt, Phys_address, IBS_Phy_Address,
BillAddress1, IBS_Bill_address,
TmpCity, ibs_City, Tmp_Name, IBS_Name, tmpMAC_Code, Mac_code,
Ibs_DUD, Taxpayer_Registration_Code, tmp_Ph, contact_ph
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-24 : 14:46:40
I modified the query to add another CTE to help us pick the best match. See below, I have comments in the code.

-- step 1. After you have created and populated #tmp, add a column to it
-- with this command. We are adding a serial number column, which we will need.
alter table #tmp add SNMId int not null identity(1,1);

-- now do the CTE thingie just as before, but include the SNMId in the select
-- list. See the below in red.
with cte as
(
select
case when n.Physical_address_1 = m.Phys_add_1 then 1 else 0 end +
case when n.accnt_nm = m.accnt_nm then 1 else 0 end +
case when n.City = m.Phys_city then 1 else 0 end +
case when n.Major_accnt_cd = m.mac_code then 1 else 0 end +
case when n.[IBS DUD] = m.Taxpayer_Registration_Code then 1 else 0 end +
case when n.cntct_ph_no = m.contact_ph then 1 else 0 end +
case when n.billaddress1 = m.Bill_add_1 then 1 else 0 end as MatchingFieldCount,

-- put pretty much any column from #tmp or #tmp3 that you want.
n.accnt_no as TmpAccnt,
m.accnt_no as IBS_Accnt,
n.Physical_address_1 as Phys_address,
m.Phys_add_1 as IBS_Phy_Address,
n.Billaddress1 as BillAddress1,
m.bill_add_1 as IBS_Bill_address,
n.City as TmpCity,
m.BillCity as ibs_City,
n.accnt_nm as Tmp_Name,
m.accnt_nm as IBS_Name,
n.Major_accnt_cd as tmpMAC_Code,
m.MAC_code,
n.[IBS DUD] as ibs_DUD,
m.Taxpayer_Registration_Code,
n.cntct_ph_no as tmp_Ph,
m.contact_ph,
n.SNMId
from
#tmp n cross join #tmp3 m
),
-- Now we want to order the result set from the cte to pick the best match.
cte2 as
(
select
row_number() over (partition by SNMId order by MatchingFieldCount desc) as rowId,
MatchingFieldCount*100/7.0 as BestMatchPercent,
*
from
cte
)
-- And, the best match would have rowId = 1, so select that.
select
*
from
cte2
where rowId = 1;


If you want the best and second best, you can change the where clause to "where rowId in (1,2)".

If there are two rows in the main table that gave the best match, this will pick only one of those (at random). If that becomes a problem, instead of row_number, we can use dense_rank etc.

But, if you can get this to work, refinements are not too hard.

PS: "I actually am very excited"
You are turning into an SQL junkie just like the rest of the people on this forum, ahmed!! :--)
Go to Top of Page
    Next Page

- Advertisement -