?
Solved

Help with  SQL Update query

Posted on 2011-02-28
9
Medium Priority
?
579 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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 

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 2000 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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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