Solved

Help with  SQL Update query

Posted on 2011-02-28
9
570 Views
Last Modified: 2012-05-11
Hi Experts!

I have 2 tables with customers
Tab_Customers have the following record layout.

Customerno,
Firstname,
lastname,
adresse,
Zip,
City,
Phone,

Tab_Phone has the following recordlayout

Tab_Phone
Customerno
Firstname,
lastname,
adresse,
Zip,
City,
Phone

In Tab_Customers there is no phone numbers present
In Tab_Phone there are no customernumbers present, but it has phone numbers present.

I would like to add the customer number to the Tab_Phone table where ever there is a big similarity on the name.

I have tried the following:

update Tab_Phone
set Customerno = (
select c.Customerno from Tab_Customers c where exists(select * from Tab_Phone p where
similar (p.Firstname,c.firstname)<99 and
similar (p.Lastname,c.lastname)<99 and
similar (p.Adresse,c.adresse)<99 and
similar (p.zip,c.zip)<99 and
similar (p.City,c.City)<99)
)

Is someone able to help me?

regards
gfk76
0
Comment
Question by:gfk76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 25

Expert Comment

by:reb73
ID: 34998903
Do you have a UDF called similar or are you trying to hint that you'd like to use a function that will act on similarity?

You could try the query below but it tries to match based on how the names sound when pronounced and may not be very accurate -

 
update p
set Customerno = c.customerno
from  Tab_Phone p
inner join Tab_Customers c on
	difference(p.Firstname, c.firstname)	> 3
and	difference(p.Lastname,c.lastname)	> 3
and	difference(p.Adresse,c.adresse)		> 3
and	difference(p.zip,c.zip)			> 3
and	difference(p.City,c.City)		> 3

Open in new window

0
 

Author Comment

by:gfk76
ID: 34999604
Hi reb73!

Thank you for trying to help.
I should probably told you that it is a Sybase database.
Similar is a built in function in iAnywhere, that checks for similareties.

I tryed your query with similar instead of difference

update p
set Customerno = c.CustomerNo
from  Tab_Phone p
inner join Tab_Customers c on
    similar (p.Firstname,c.firstname)<99 and
    similar (p.Lastname,c.lastname)<99 and
    similar (p.Adresse,c.adresse)<99 and
    similar (p.zip,c.zip)<99 and
    similar (p.City,c.City)<99)

I get following error message
"Table p not found"

Any idea?
0
 
LVL 25

Expert Comment

by:reb73
ID: 34999673
Try changing the first line from

update p

to

update Tab_Phone
0
Stressed Out?

Watch some penguins on the livecam!

 

Author Comment

by:gfk76
ID: 35000127
This updates with the same customer number on all rows
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35002091
Can you check this?
update Tab_Phone
   set Tab_Phone.Customerno = Tab_Customers.CustomerNo
  from Tab_Phone,Tab_Customers 
 where similar (Tab_Phone.Firstname,Tab_Customers.firstname)<99 and
       similar (Tab_Phone.Lastname,Tab_Customers.lastname)<99 and
       similar (Tab_Phone.Adresse,Tab_Customers.adresse)<99 and
       similar (Tab_Phone.zip,Tab_Customers.zip)<99 and
       similar (Tab_Phone.City,Tab_Customers.City)<99)

Open in new window

0
 
LVL 25

Accepted Solution

by:
reb73 earned 500 total points
ID: 35005950
If you check for a result value below 99 for the similar function, you will indeed get the same customer code for all rows.. It will end up matching the first record (not necessarily the same but likely) for all record sin Tab_Phone.

See the function reference below -

http://www.mssqltips.com/tip.asp?tip=1782

I believe you should be checking for >99 in the where clause -

(Suggestion: Start with >99 and reduce by one until 90, review the code below which resets the customerno and then loops through until it can populate as many customerno values as possible, you may still have to disable the matching on zip, city and address eventually to get a good population)
update Tab_Phone set Customerno = null -- This will reset the customer no initially

Declare @matchlevel int
set @matchlevel = 99

while @matchlevel >= 90
begin
	update p
	set Customerno = c.CustomerNo
	from  Tab_Phone p
	inner join Tab_Customers c on
	    similar (p.Firstname,c.firstname)> @matchlevel and
	    similar (p.Lastname,c.lastname)> @matchlevel and
	    similar (p.Adresse,c.adresse)> @matchlevel and
	    similar (p.zip,c.zip)> @matchlevel and
	    similar (p.City,c.City)> @matchlevel)
	where p.Customerno is null

	set @matchlevel = @matchlevel - 1
end

Open in new window

0
 
LVL 25

Expert Comment

by:reb73
ID: 35005956
Correction: Please change line 8 as follows -

update Tab_Phone
0
 

Author Closing Comment

by:gfk76
ID: 35005979
Arggghhhh.....

Thank you so much......
I have been working on this query for 1 and a half day now.....
I cant belive I missed it.

You have saved my day, and tonight my sleep :-)
0
 
LVL 25

Expert Comment

by:reb73
ID: 35006043
Glad to help! Cheers..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question