Question

I am trying to build a query that will look up a product_subcategory based on a reference data in a user entered table and joining two tables together. My database is SQL Server 2012 Express.

First the products table has three columns: Product_id (unique identifier), event_id (INT data type), and product_category. (product_category needs to be alphanumeric currently varchar(32) data type)

Example Products table data:

Product_id  event_id    product_category
1           20          100
2           20          105
3           20          200
4           21          100
5           21          200
6           21          203
7           22          105
8           22          207

Second the events table has two columns: event_id (unique identifier, INT Data type) and zone (float data type, not sure why this was setup as float, probably should have been INT but its a pre-existing table and I don't want to change it)

event_id      zone
20            1
21            2
22            3

Third the subcategory table has four columns: subcategory_id (unique identifier, INT data type), zone (joins to zone column in products table, INT Data type), category_lookup (varchar(max) data type), and product_subcategory (varchar(50) data type). This is a table that I am creating for this project so I can change the structure or datatypes to be whatever is needed for the project, I don't have that flexibility on the other tables.

Example Subcategory table data:

subcategory_id  zone    category_lookup    product_subcategory
1               1       '1%'               25
2               1       '2%'               23
3               2       '1%'               26
4               2       '2%'               30

I want to build a query that will search the product table and match a zone, product_category, and product_subcategory together based on the value in the subcategory.category_lookup column.

The data that I want returned from the query is:

product_ID  zone    product_category    product_subcategory
1           1       100                 25
2           1       105                 25
3           1       200                 23
4           2       100                 26
5           2       200                 30
6           2       203                 30
7           3       105                 NULL or 'N/A'
8           3       107                 NULL or 'N/A'

The logic behind looking up the matching subcategory will be similar to below: (this is essentially what is stored in the subcategory table) (the text in the “quotes” is what I mean by reference data, and will be user entered)

IE... if zone = 1 and product_category “begins with 1” then product_subcategory = 25
IE... if zone = 1 and product_category “begins with 2” then product_subcategory = 23
IE... if zone = 2 and product_category “begins with 1” then product_subcategory = 26
IE... if zone = 2 and product_category “begins with 2” then product_subcategory = 30

I do understand that one of the issues with my logic is that if multiple subcategories match to one product then it will throw an error, but I think I can code around that once I get this part of it working

I am fine going a different direction with this project but this is the first way I decided to tackle it. The most important component is that the product_subcategory’s are going to be located in a separate user entered table, and there needs to be user entered logic as discussed above to determine the product_subcategory based on zone and product_category.

I am not a SQL guru at all so I don’t even know where to start to handle this problem. Any advice is appreciated.

Based on answers I have received so far I have come up with this:

SELECT p.product_id, p.event_id, e.zone, p.product_category, sc.product_subcategory
FROM Products p
LEFT JOIN events e on p.event_id = e.event_id
LEFT JOIN SubCategory sc ON e.zone = sc.zone AND CAST(p.product_category as varchar(max)) like sc.category_lookup

But unfortunately its only returning NULL for all of the product_subcategory results.

Any additional help is appreciated. Thanks,

Was it helpful?

Solution

This should do the trick, you will just need to modify the CAST(p.zone as nchar(10)) to insert the correct data type for your category_lookup column, in place of nchar(10), as I assume the zone in Products is an int where as the lookup column is some string based column:

SELECT p.product_id, p.zone, p.product_category, sc.product_subcategory
FROM Products p
LEFT JOIN SubCategory sc ON p.zone = sc.zone
AND CAST(p.zone as nchar(10)) like sc.category_lookup

Based on your updates, the following should work:

SELECT p.product_id, p.event_id, e.zone, p.product_category, 
       sc.product_subcategory
FROM Products p
INNER JOIN events e on p.event_id = e.event_id
LEFT OUTER JOIN SubCategory sc ON e.zone = sc.zone 
     AND CAST(e.zone as nchar(250)) LIKE CAST(sc.category_lookup as nchar(250))

Update based on comments:

SELECT p.product_id, p.event_id, e.zone, p.product_category, 
       sc.product_subcategory
FROM Products p
INNER JOIN events e on p.event_id = e.event_id
LEFT OUTER JOIN SubCategory sc ON e.zone = sc.zone 
     AND CAST(p.product_category as nchar(250)) LIKE sc.category_lookup

Working sample SQLFiddle

OTHER TIPS

Not tested but is this what you're looking for ?

select a.product_ID, a.zone, a.product_category, b.product_subcategory
from Products a
inner join Subcategory on ((a.zone = b.zone) and (a.product_category like b.category_lookup))

Try this..

select p.product_id, p.zone, p.product_category, isnull(s.product_subcategory,'NA') as product_subcategory
from Products p
left outer join Subcategory s on (s.zone = p.zone and p.product_category like s.category_lookup);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top