简体   繁体   中英

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?

You can join both tables with

 INNER JOIN ON (concat(domestic.itemid, '-h') = international.itemid)

You might have to adjust this for the Ms Access SQL dialect. I think for concat you can use &, IIRC.

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM