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
Solved

Help with  SQL Update query

Posted on 2011-02-28
9
557 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
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article describes some very basic things about SQL Server filegroups.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

790 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