Link to home
Start Free TrialLog in
Avatar of jazz__man
jazz__man

asked on

I need help to create an update statement with select and case involved

Hi,

I need to do an update to a Company column in my database.

Basically I have a lookup table like so


Lookup Table
-----------------
LookupID
Category eg TravelCompany,AccommodationCompany etc
Description eg The names of the companies

and another table that has companies in it like so

Accommodation table
---------------------------
AccommodationName
Company
CompanyID

Now the problem is that in the Accommodation table the companies vary slightly from the lookup table values. The CompanyID column is a new column and does not have any data in it at present so this is the reason for the update. I hope this is starting to make some sense, I need a kind of update select case procedure that will allow me to insert the correct id's into Accommodation table.

Your help would be appreciated



Avatar of zvytas
zvytas
Flag of United Kingdom of Great Britain and Northern Ireland image

UPDATE Accommodation
SET CompanyID = (SELECT LookupID FROM Lookup WHERE Lookup.Description = Accommodation.Company)

OR

UPDATE Accommodation
SET CompanyID = Lookup.LookupID
FROM Accommodation acc
   INNER JOIN Lookup ON acc.Company = Lookup.Description

These queries were written assuming Lookup table does not contain duplicate Descriptions.
Avatar of jazz__man
jazz__man

ASKER

what about where Lookup.Description <> Accommodation.Company
Hi Jazz man,

from my understanding you effectively have dirty data, i.e. the names of the companies vary slightly to the descriptions, is this correct?

If so, depending on how involved you want to go there are a number of possible solutions but no "black & white" answer.

The easiest thing to do is probably use an SSIS fuzzy lookup component to match the ones that arent exactly the same.

Beyond that you are getting into the realms of doing things like leveinsten differences and phonetics


Thanks

Dave
Cases like that will have to be handled manually, OR using soundex:

http://databases.about.com/od/development/l/aasoundex.htm
Avatar of awking00
Can you provide some sample data for the two tables and what the accommodation table should look like after the update?
DavidMorrison

Thats exactly what I have...dirty data.

To my knowledge I need some kind of case statement
ASKER CERTIFIED SOLUTION
Avatar of DavidMorrison
DavidMorrison
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for hijacking,

Jazz_man, can you please clarify the issue @

https://www.experts-exchange.com/questions/27244357/Links-on-masterpage-are-not-working.html

(that you asked some time ago, which opened and closed a few times, and you never replied)

EE admins, is there anyway to send private message to members?

Thanks...