Solved

Help with  SQL Update query

Posted on 2011-02-28
9
542 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
  • 5
  • 3
9 Comments
 
LVL 25

Expert Comment

by:reb73
Comment Utility
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
Comment Utility
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
Comment Utility
Try changing the first line from

update p

to

update Tab_Phone
0
 

Author Comment

by:gfk76
Comment Utility
This updates with the same customer number on all rows
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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
Comment Utility
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
Comment Utility
Correction: Please change line 8 as follows -

update Tab_Phone
0
 

Author Closing Comment

by:gfk76
Comment Utility
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
Comment Utility
Glad to help! Cheers..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now