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 8065683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:44:48+00:00 2026-06-05T11:44:48+00:00

I want to return all products from the Product table based on any category

  • 0

I want to return all products from the Product table based on any category in the hierarchy.

For example, you could search products by all “Fords” (category = 1) and it would return two results.

Right now, this recursive result returns the products only if there is an exact match in the product table to the CategoryHierarchy table. So it works if I say category = 4.

In the product table, I have two Ford Mustangs. Only the price and description are different (not shown here), but their classification is the same in terms of the product categories.

[Product Table]
[ProductId] [ProductName] [CategoryId]
1       Ford Mustang 1     4
2       Ford Mustang 2     4
3       Buick Regal 3      12

Each Ford Mustang has the CategoryHierarchy that goes 1,2,3,4. Those same two results should show up if the Sql parameter lowestcategory is 1 OR 2 OR 3 OR 4. Right now, it only works if it = 4.

[CategoryHierarchy]
[Id]    [parentId]  [categoryName]
1       0           Ford
2       1           Mustang
3       2           2010
4       3           Blue
10      0           Buick
11      10          Regal
12      11          1999

MS SQL 2008:

declare @lowestcategoryid int;

set @lowestcategoryid = 4;--returns the products but I need it to work if this var = 1,2,3 too

--recursive loop
with RecursiveResult( id, CategoryName, ProductName, parentId) as
(
select ch.Id, ch.categoryname, p.productname, ch.parentId
from CategoryHierarchy ch 
join product p on p.categoryId = ch.id
where ch.id = @lowestcategoryid

UNION ALL

select p.categoryId, ch.categoryName, p.productname, ch.parentId
from product p
join CategoryHierarchy ch on p.categoryId = ch.id
join RecursiveResult r on ch.parentId = r.Id 

)

select * from recursiveresult
  • 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-05T11:44:50+00:00Added an answer on June 5, 2026 at 11:44 am

    I think you want to separate out the recursive portion (traversing the CategoryHierarchy table) from the join on Product. Start at the lowest (or “highest”) category and move deeper, and finally join that set on Product… like this:

    DECLARE @lowestcategoryid INT;
    
    SET @lowestcategoryid = 3;
    
    --recursive loop
    WITH RecursiveResult( id, CategoryName, parentId) AS
    (
    SELECT ch.Id, ch.categoryname, ch.parentId
    FROM CategoryHierarchy ch 
    WHERE ch.id = @lowestcategoryid
    
    UNION ALL
    
    SELECT ch.Id, ch.categoryname, ch.parentId
    FROM CategoryHierarchy ch 
        JOIN RecursiveResult r 
            ON r.Id = ch.ParentId
    
    )
    
    SELECT * 
    FROM recursiveresult r
        JOIN Product p
            ON p.CategoryId = r.Id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I simply want to return all rows from a users table but selected fields,
I have a products table, with the fields product, category and cost, of type
I'm trying to select products based on facets for a product category page. The
i have product table it contains all products details . product_id product_type_id internal_name 1001
The scenario: Table1 CatId|Name|Description Table2 ItId|Title|Date|CatId (foreign key) I want to return all rows
All I want to do is return the index of the i that is
I want to iterate over all childs of a jQuery's .children() return value, like
I want to implement all my own rotation animations but if I only return
I want to return a private field from a remoted object but i get
I want to return a set of values from function till the point they

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.