1

I am in the process of creating an Oracle DB conversion script to move records from an old singular table to a new table with more fields as well as child/reference tables for better integrity. When I run a SELECT DISTINCT for VENDOR in my Table1, I get back 58 results. An example of how varied these results are for the same meaning is below:

  • WWT
  • &nbspWWT
  • Worldwide Technologies
  • &nbspWorldwide Technologies
  • WorldWide Technology
  • World Wide Technology

In my reference table, I have already setup a record for this particular value as "World Wide Technology, Inc."

Is there a way I can specify (maybe a CASE statment?) that when these various spellings are used to return the value I have in my child table with (ex.) "World Wide Technology, Inc."?

This Table is the simplest child table to tackle as it only has 17 distinct values I've plugged into it, whereas Table1 has 58 distinct rows returning. So far I have:

INSERT INTO Table2 (VendorID, col2, col3, col4, etc...)
SELECT T3.ID, T1.col7, T1.col8, T1.col9, etc...
FROM Table1 T1
INNER JOIN Table3 T3 ON LTRIM(UPPER(T1.Vendor)) = UPPER(T3.Vendor_Name)

I've got 1349 records in Table1, but this (for just the SELECT portion) is only returning 418 records.

Does anyone know how to go about what I'm after here?

2
  • Can you create a look-up table for the duration of your migration? If it has two columns, one containing all the distinct values from the old table, the other the sane value from your new look-up table (which would be duplicated), you could then join on that, and drop it when you've finished? Otherwise yes, you could use a big case statement to do the same thing.
    – Alex Poole
    Apr 15, 2015 at 17:10
  • I'd really prefer not to get into extra tables if possible. Would you be able to provide an example with the CASE statement in this scenario to get me started? Apr 15, 2015 at 18:12

2 Answers 2

3

If you don't have and don't want to create a mapping table, the case approach would be similar to your earlier question about dates. To convert all values you'd do something like:

select case
  when vendor in (
    'WWT',
    ' WWT',
    'Worldwide Technologies',
    ' Worldwide Technologies',
    ' WorldWide Technology',
    'World Wide Technology'
  ) then 'World Wide Technology, Inc.'
  when t1.vendor in (
    'ACME',
    ' acme'
  ) then 'ACME, Inc.'
  ... other groups of original values with their new equivalents
  else null
  end as vendor_name
from t1;

In your case you'd probably hard-code the new vendor_id rather than the name, otherwise you'd just have to join back to t3 to get the ID based on your mapped name:

INSERT INTO Table2 (VendorID, col2, col3, col4, etc...)
SELECT case
  when t1.vendor in (
    'WWT',
    ' WWT',
    'Worldwide Technologies',
    ' Worldwide Technologies',
    ' WorldWide Technology',
    'World Wide Technology'
  ) then 42 -- ID for 'World Wide Technology, Inc.'
  when t1.vendor in (
    'ACME',
    ' acme'
  ) then 76 -- ID for 'ACME, Inc.'
  ... other groups of original values with their new equivalents
  else null
  end as vendor_id, T1.col7, T1.col8, T1.col9, etc...
FROM Table1 T1;

If you have a lot of distinct values that only differ by case and leading/trailing whitespace, you could reduce the number of values to check with something like:

select case
  when trim(upper(t1.vendor)) in (
    'WWT',
    'WORLDWIDE TECHNOLOGIES',
    'WORLD WIDE TECHNOLOGY'
  ) then 42 -- ID for 'World Wide Technology, Inc.'
  when trim(upper(t1.vendor)) in (
    'ACME'
  ) then 76 -- ID for 'ACME, Inc.'
  else null
  end as vendor_name
from t1;

You could potentially also remove punctuation etc.. Basically whatever query expression you use to identify the distinct values in the first place has to match whatever you use in the case expressions. So in this example, rather than your original SELECT DISTINCT VENDOR FROM Table1 which got 58 values you'd do SELECT DISTINCT TRIM(UPPER(VENDOR)) FROM Table1 which will give you fewer, reducing (slightly) the pain of manually matching each one to a new vendor ID.

If you want to use the description in the case mappings, you can join to your new look-up table, and then do the case in the join clause:

select t1.vendor, t3.vendor_id, t3.description
from t1
left join t3 on t3.description = case
  when trim(upper(t1.vendor)) in (
    'WWT',
    'WORLDWIDE TECHNOLOGIES',
    'WORLD WIDE TECHNOLOGY'
  ) then 'World Wide Technology, Inc.'
  when trim(upper(t1.vendor)) in (
    'ACME'
  ) then 'ACME, Inc.'
  else null
  end;

VENDOR                    VENDOR_ID DESCRIPTION               
------------------------ ---------- ---------------------------
 Worldwide Technologies          42 World Wide Technology, Inc.
 World Wide Technology           42 World Wide Technology, Inc.
WWT                              42 World Wide Technology, Inc.
 AcMe                            76 ACME, Inc.                 

etc. This is just a demo, obviously. I've made it a left join so if you have a value you haven't mapped, or have a typo in a description, etc. it will try to insert a null value. You could then either look for nulls and fill them in as needed, or have a no-null constraint on your new (foreign key, presumably) column so it won't let you insert without a match - but that might be too restrictive, again depending on your actual data.

8
  • Thanks Alex, I'll give it a shot! And yea, I was just considering what you mention in your EDIT. I'm not sure if it would be any easier to have the script look up the ID of the desired value, or just hardcode the ID value in at the end of the CASE. Apr 15, 2015 at 18:23
  • if you need it more than once, you can write a function using this. and you can use TRIM and UPPER to include leading and trailing spaces and differences in case.
    – 1010
    Apr 15, 2015 at 18:29
  • Personally I'd probably use a query to list all the values and set up the case clauses directly from that, rather than edit the list so it can be used with trim and/or upper (or manually type the values); but yes, that is an option. It depends on the volume of data I suppose.
    – Alex Poole
    Apr 15, 2015 at 18:52
  • @AlexPoole, Thanks! This example seems to have done the trick. Would you mind EDITING in an example of this other method you and 1010 are mentioning? For this child table (Vendors) there were only 58 distinct values that had to cased for the correct value, but for my (Models) child table there are vastly more to contend with. Apr 15, 2015 at 19:58
  • 1
    @AnalyticLunatic - I've added a variation using trim and upper, which I think covers off what 1010 and I were talking about. How much it helps you depends on your actual data though.
    – Alex Poole
    Apr 16, 2015 at 8:55
0

I think you are looking for a LEFT OUTER JOIN. Lets then assume that you have a mapping table Table4 with columns wrong to represent a mispelling of the entity you want and right to represent the correct spelling. Then:

INSERT INTO Table2 (VendorID, col2, col3, col4, etc...)
SELECT COALESCE ( T4.RIGHT, T3.ID, T1.Vendor ), T1.col7, T1.col8, T1.col9, etc...
FROM Table1 T1
LEFT OUTER JOIN Table3 T3 ON LTRIM(UPPER(T1.Vendor)) = UPPER(T3.Vendor_Name)
LEFT OUTER JOIN Table4 T4 ON LTRIM(UPPER(T1.Vendor)) = UPPER (T4.WRONG)

The JOIN will include all records from T1 and results from T3 or T4 only where the match condition in either JOIN is met. Where the condition is not met, all T3/T4 columns will appear to be NULL in the query results. Thus, you could use COALESCE in the SELECT clause to say: if there is a T4.right, use that, otherwise if there is a T3.ID, use that, otherwise, use T1.Vendor.

5
  • Ok, that is returning all 1349 rows now! But what about the issue of there being no match due to all the different spellings? How would I (assuming a CASE statement?) go about checking the multiple spellings and if found returning/inserting the value I want from the child table? Like if VENDOR = WWT I'd want the Vendor.ID to match the ID in my other table where the VENDOR_NAME = World Wide Technologies, Inc.? Apr 15, 2015 at 16:41
  • Don't you have all of the alternative spellings in T3? If not, why not?
    – Politank-Z
    Apr 15, 2015 at 16:42
  • The old database using the singular table is a mess with different spellings for the same value, etc. I've set up these child/reference/lookup tables in addition to my main NEW table in order to better set some DB Integrity. Instead of just entering random Text, users will be forced either to select from those available values in the reference table, or create a new one there to select it. Ultimately it will be user discretion to create new/select from the list, but in this case these multiple values all reference the SAME entity and should have 1 value to represent it--not multiple spellings. Apr 15, 2015 at 16:48
  • 1
    OK, I misunderstood the function of T3. Perhaps you need a T4 to map the alternative spellings to the proper entity.
    – Politank-Z
    Apr 15, 2015 at 16:50
  • Surely there must be a way to check the spellings for the INSERT and (if found) INSERT the ID of the value I actually want represented in the field? Apr 15, 2015 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.