Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to return a single value from multiple values based on mapping

$
0
0

I have the following tables:

Category (has two columns "Input", "Mapping")

OJOrange Juice
APPApple Juice

So OJ should be mapped to Orange Juice and so on.

Here is the main table: Product

Product (has the following columns:ID,  ProdGroup) and possible values are:

1OJ
2OJ|APP
3APP|CRN

Now, what I am trying to do is to join the two tables based on Category.Input vs. Product.ProdGroup; however, the problem is that Product.ProdGroup could have multiple values and in this case, just use the first item for matching.  For example,

OJ|APP -> Just use OJ, which is mapped to OJ in Category table and it should return Orange Juice

I was thinking a query like this:

SELECT PR.ID, PR.ProdGroup, CAT.Mapping FROM Product PR

INNER JOIN Category CAT ON PR.ProdGroup LIKE '%' + CAT.Input + '%'

Expected result:

1OJOrange Juice
2OJ|APPOrange Juice
3APP|CRNApple Juice

Unfortunately, it does not seem to work the way I would like.

Any suggestions would be greatly appreciated.

Thanks.


SK


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>