Solved

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

Posted on 2011-09-07
8
251 Views
Last Modified: 2012-05-12
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



0
Comment
Question by:jazz__man
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 5

Expert Comment

by:zvytas
ID: 36495594
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.
0
 
LVL 2

Author Comment

by:jazz__man
ID: 36495635
what about where Lookup.Description <> Accommodation.Company
0
 
LVL 5

Expert Comment

by:DavidMorrison
ID: 36495690
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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:zvytas
ID: 36495692
Cases like that will have to be handled manually, OR using soundex:

http://databases.about.com/od/development/l/aasoundex.htm
0
 
LVL 32

Expert Comment

by:awking00
ID: 36495710
Can you provide some sample data for the two tables and what the accommodation table should look like after the update?
0
 
LVL 2

Author Comment

by:jazz__man
ID: 36496019
DavidMorrison

Thats exactly what I have...dirty data.

To my knowledge I need some kind of case statement
0
 
LVL 5

Accepted Solution

by:
DavidMorrison earned 500 total points
ID: 36496287
Jazz man,

If you had a list of all possible combinations of the company name and what they should match to then a IN statement might work rather than a case, although I get the impression from your post that you don't have this mapping list?

As I said before, your best bet is SSIS, pipe it through a fuzzy lookup component, this will get you so far. other than that it will be a manual process. If you only have to do this once and the data volumes arent that large then I'd just bite the bullet and whack it excel and do it by hand, creating a mapping table which you could then load into sql and just do a simple join.



As I said unfortunately there is no real "black and white" answer to fuzzy matching
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36499238
Sorry for hijacking,

Jazz_man, can you please clarify the issue @

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27244357.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...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

11 Experts available now in Live!

Get 1:1 Help Now