Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:42:06+00:00 2026-06-14T08:42:06+00:00

Ok so I have some data in a product table that contains a category

  • 0

Ok so I have some data in a product table that contains a category id column that only contains 2 of the 4 available characters used in the category list table.

Example: product has category ID ‘AA’ but could be from either the Desktops (AAA) or Server (AAB) categories.

In the category table we have 3 columns that contain info to help us: include, atrID and valID. The valID column contains a comma-separated list of values (e.g. ‘K01819’ or ‘K00846,K00851’) that may contain only one ID or more. The atrID column contains a similar string (e.g. ‘A00432’). The include column is either 1 or 0 based on some work done by a programmer that came and went long ago.

Basically he had trawled through the various tables and found that for certain categories (e.g. AA) we could join another table from the category table and use the atrID and list of valIDs to tie it all in.

Example:

Product “a” has a catID of AA and is a desktop machine. This belongs in category ‘AAA’ which has 0 for include, ‘A00432’ for atrID and ‘K01819’ for valID.

Example SQL to get category info:

SELECT cat.atrID, cat.valID, cat.[include]
FROM db.dbo.cat AS cat
WHERE cat.catID = 'AAA'

I would then save these variables in the .Net code and build the SQL depending on the value of include. I would also split the valID up and pass this in as individual parameters resulting in the following code being executed on the SQL server.

SELECT DISTINCT prod.prodID
FROM db.dbo.prod AS prod
INNER JOIN db.dbo.atr AS atr ON atr.prodID = prod.prodID
WHERE prod.catID = 'AA'
AND atr.atrID = 'A00432'
AND atr.valID NOT IN('K01819')

To change this for servers I would change the NOT IN to an IN as servers (AAB) have an include value of 1. Example:

SELECT DISTINCT prod.prodID
FROM db.dbo.prod AS prod
INNER JOIN db.dbo.atr AS atr ON atr.prodID = prod.prodID
WHERE prod.catID = 'AA'
AND atr.atrID = 'A00432'
AND atr.valID IN('K01819')

I use IN and NOT IN because some of the categories have more than one value in the valID column.

My question is, if I want to get the category for a specific product without previously knowing it, is it even possible? (Failing getting a list of all the products from the categories you think it’s in and matching it in one or another of the lists). I’ve been telling my boss I can’t figure it out for days now and I’m not getting anywhere. Any help would be greatly appreciated.

EDIT:

What I’m trying to do is get the category for any product. I included the above SQL as an example of how I get a list of products for the Desktop and Server categories.

I want the reverse, i.e. getting the 3 letter category from the categories table when I only have the two letter category from the product table.

EDIT 2:

Product table columns:
prodID varchar(40)
catID char(2)

Sample rows:
‘S101010’, ‘AA’ (desktop)
‘S202020’, ‘AA’ (server)
‘S303030’, ‘ED’ (laser printer)
‘S404040’, ‘ED’ (inkjet printer)

Category table columns:
catID varchar(4)
description varchar(50)
include bit (actually a tinyint which should go to show what I deal with on a daily basis =P)
atrID varchar(6)
valID varchar(250)

Sample rows:
‘AAA’, ‘Desktops’, 0, ‘A00432’, ‘K01819’
‘AAB’, ‘Servers’, 1, ‘A00432’, ‘K01819’
‘EDA’, ‘Laser Printers’, 1, ‘A00172’, ‘K00846,K00851’
‘EDB’, ‘Inkjet Printers’, 1, ‘A00172’, ‘K00845’

Attribute table columns:
prodID varchar(40)
catID char(2)
atrID varchar(10)
valID varchar(10)

Sample rows:
‘S101010’, ‘AA’, ‘A00432’, ‘K01817’
‘S202020’, ‘AA’, ‘A00432’, ‘K01819’
‘S303030’, ‘ED’, ‘A00172’, ‘K00846’
‘S303030’, ‘ED’, ‘A00172’, ‘K00851’
‘S404040’, ‘ED’, ‘A00172’, ‘K00845’

What I have included above is how I get a list of products when I know category.catID. What I want is to be able to get the category when I only have product.prodID and product.catID.

I’m after something like this but it’s incomplete and won’t work as it is. I would then check the result of catIn.catID and catNotIn.catID to see which one wasn’t null and that would be my category but I can’t figure out the joins. If it is even possible. And it doesn’t take into account the categories that have more than two variants.

SELECT prod.prodID, catIn.catID, catNotIn.catID
FROM product AS prod
INNER JOIN attributes AS atr ON atr.prodID = prod.prodID
LEFT OUTER JOIN category AS catIn ON LEFT(catIn.catID, 2) = prod.catID AND catIn.atrID = atr.atrID --more conditions needed here
LEFT OUTER JOIN category AS catNotIn ON LEFT(catNotIn.catID, 2) = prod.catID AND catIn.atrID = atr.atrID --more conditions needed here

I hope this explains what I’m after a bit better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:42:08+00:00Added an answer on June 14, 2026 at 8:42 am

    I’ll start by saying your data model is pretty bad. You shouldn’t have a comma-list of values where you need to then split and match against. It should be a table relationship. But I digress…

    So if the only difference is whether or not you use IN vs NOT IN based on the value of include then if I were to write this wholly on the server I would do this:

    First, get yourself a nice CSV split UDF. how to split and insert CSV data into a new table in single statement? This is a good example — nice and simple.

    Then, the query (which I would turn into a stored procedure) would likely look something like this:

    CREATE PROCEDURE SprocNameHere
    @prodID INT
    AS
    
    -- I'm making assumptions about the data types here.  Fix them accordingly
    DECLARE @atrID INT
    DECLARE @valID varchar(MAX)
    DECLARE @include BIT
    DECLARE @catID varchar(50)
    
    -- given the passed in product ID, look up @catID
    SELECT @catID = catID FROM prod WHERE prodID = @prodID
    
    SELECT @atrID = cat.atrID, @valID = cat.valID, @include = cat.[include]
    FROM db.dbo.cat AS cat
    WHERE cat.catID = @catID
    
    
    SELECT DISTINCT prod.prodID
    FROM db.dbo.prod AS prod
    INNER JOIN db.dbo.atr AS atr ON atr.prodID = prod.prodID
    LEFT JOIN dbo.inline_split_me(',', @valID) ism ON ism.Value = atr.valID
    WHERE prod.catID = 'AA' -- is this hardcoded or should this come from the query above??  How do you convertt 'AAA' to 'AA'?  What Table/relationship/concept does this?
    AND atr.atrID = @atrID
    AND ((@include = 0 AND ism.Value IS NULL) OR (@include = 1 AND ism.Value IS NOT NULL))
    

    Very hard to make sure my syntax above is accurate as I don’t have a full working model. But here’s the general idea: Your UDF inline_split_me splits records into a “table” which you can then join against. By using LEFT JOIN, if there’s not a match between the atr table, then ism.Value will be null. This can be used to do NOT IN. The opposite is, then, also true for the IN simulation.

    EDIT 1: Based on your feedback, Here’s a test bed I came up with. I don’t have a complete view of the data, but I think this is what you are looking for (or at least getting close). Comment and I’ll adjust.

    Given your sample data, I ran this in my tempdb. Since I didn’t have an Attribute record for EDB I can’t be certain if I’m getting back what I’m supposed to get back. I also get back two records S303030, one for each of the valIDs in the Attribute table. You can use DISTINCT to get back just one if you’re looking only for uniqueness across ProdID + Category.CatID, or if there’s a piece of the puzzle I didn’t understand, let me know and I’ll tweak my answer. This works for everything except Ink Jets, because your sample data didn’t have an inkjet record in the #Category table. If I fake one with say UNION SELECT 'S404040', 'ED', 'A00172', 'K00845' then it seems to work.

    --DROP TABLE #Product
    --DROP TABLE #Category
    --DROP TABLE #Attribute
    
    --DROP FUNCTION inline_split_me
    
    GO
    CREATE FUNCTION inline_split_me (@SplitOn char(1),@String nvarchar(max))
    RETURNS @results TABLE(Part NVARCHAR(MAX))
    AS
    BEGIN
    
        IF (CHARINDEX(@SplitOn,@String)-1<= 0)
            INSERT INTO @results
            SELECT @string AS Part
        ELSE
            WITH SplitSting AS
                (
                SELECT
                    LEFT(@String,CHARINDEX(@SplitOn,@String)-1) AS Part
                        , CASE WHEN LEN(@String)-CHARINDEX(@SplitOn,@String) > 0 THEN RIGHT(@String,LEN(@String)-CHARINDEX(@SplitOn,@String)) ELSE NULL END AS Remainder
                    WHERE @String IS NOT NULL AND CHARINDEX(@SplitOn,@String)-1>0
                UNION ALL
                SELECT
                    LEFT(Remainder,CHARINDEX(@SplitOn,Remainder)-1)
                        ,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(@SplitOn,Remainder))
                    FROM SplitSting
                    WHERE Remainder IS NOT NULL AND CHARINDEX(@SplitOn,Remainder)-1>0
                UNION ALL
                SELECT
                    Remainder,null
                    FROM SplitSting
                    WHERE Remainder IS NOT NULL AND CHARINDEX(@SplitOn,Remainder)=0
                )
                INSERT INTO @results
                SELECT Part FROM SplitSting
    
    RETURN
    END
    
    GO
    
    
    CREATE TABLE #Product
    (
        prodID varchar(40)
        , catID char(2)
    )
    
    CREATE TABLE #Category
    (
        catID varchar(4)
        , description varchar(50)
        , include bit --(actually a tinyint which should go to show what I deal with on a daily basis =P)
        , atrID varchar(6)
        , valID varchar(250)
    )
    
    CREATE TABLE #Attribute
    (
        prodID varchar(40)
        , catID char(2)
        , atrID varchar(10)
        , valID varchar(10)
    )
    
    INSERT INTO #Product
    SELECT 'S101010', 'AA'
    UNION SELECT 'S202020', 'AA'
    UNION SELECT 'S303030', 'ED'
    UNION SELECT 'S404040', 'ED'
    
    INSERT INTO #Category
    SELECT 'AAA', 'Desktops', 0, 'A00432', 'K01819'
    UNION SELECT 'AAB', 'Servers', 1, 'A00432', 'K01819'
    UNION SELECT 'EDA', 'Laser Printers', 1, 'A00172', 'K00846,K00851'
    UNION SELECT 'EDB', 'Inkjet Printers', 1, 'A00172', 'K00845'
    
    INSERT INTO #Attribute
    SELECT 'S101010', 'AA', 'A00432', 'K01817'
    UNION SELECT 'S202020', 'AA', 'A00432', 'K01819'
    UNION SELECT 'S303030', 'ED', 'A00172', 'K00846'
    UNION SELECT 'S303030', 'ED', 'A00172', 'K00851'
    
    -- make sure it works with just one parameter
    SELECT * FROM dbo.inline_split_me(',', 'K00846')
    
    -- so you can see the split
    SELECT * FROM dbo.inline_split_me(',', 'K00846,K00851')
    
    -- so you can see the split in reference to the category
    SELECT c.*, Part
    FROM #Category c
    OUTER  APPLY dbo.inline_split_me(',', c.valID) split
    
    SELECT p.*, a.*, c.*
    FROM #Product p
    INNER JOIN #Attribute a ON a.prodID = p.prodID AND a.catID = p.catID
    INNER JOIN #Category c ON c.atrID = a.atrID
        AND (
                (c.include = 0 AND a.valID NOT IN (SELECT Part FROM dbo.inline_split_me(',', c.valID)))
                OR (c.include = 1 AND a.valID IN (SELECT Part FROM dbo.inline_split_me(',', c.valID)))
            )
    ORDER BY a.ProdID, a.atrID--, c.catID
    
    DROP TABLE #Product
    DROP TABLE #Category
    DROP TABLE #Attribute
    
    DROP FUNCTION inline_split_me
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel spreadsheet of some product data, (just a simple table) that
I have some data that is stored in a TIMESTAMP(6) WITH TIMEZONE column in
I have a table that contain some Inspection Data. Every commodity needs to be
I've got an SQL table that I use to keep product data. Some products
Product Version Oracle 10g I have a table in oracle with some data in
Let's say I have a Sales table, that contains columns ProductID, Date, ... some
I have some data that won't printf.... echo works, but not printf There is
I have some data that I would like to visualize. Each byte of the
Which is your preference? Let's say we have a generic Product table that has
I have a MySQL table which contains a list of all possible products a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.