Search code examples
sqlms-accessfeed

Comparing 2 items but using a value from found item


Having some issues trying to figure out how to write a SQL line to grab a value from an item. Let me explain the situation. We have 2 different databases, one for domestic and one for import. They're the same items just different product codes. The difference is just the international items have a -h at the end. So example..

12345   < domestic 
12345-h < import

We have a feed going which combines both together but the import items are missing upc codes, while the domestic items have them. So what I wanted to do was have it match both product codes and add the upc to the -h item.

Any ideas?


Solution

  • I think you want a structure like this:

    select <fields that you want>
    from ((select <list of fields from domestic>, itemid as lookupitem
           from domestic d
          ) union all
          (select <list of most fields from import>, replace(itemid, '-h', '') as lookupitem
           from import i
          )
         ) f left join
         lookup lu
         on f.lookupitemid = lu.itemid
    

    In other words, you can create the field of the right form in a subquery and then join it to the lookup table used to get UPC. I use "replace", on the assumption that some codes may not actually end with the "-h".