Solved

Help with  SQL Update query

Posted on 2011-02-28
9
562 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 54
Import text from rows into columns? 12 80
Return Rows as per Quantity of Columns Value In SQL 6 28
SQL Select Query help 1 38
Read about achieving the basic levels of HRIS security in the workplace.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

749 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