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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:27:51+00:00 2026-06-08T11:27:51+00:00

I have a Category table which has 3 fields i’m interested in: ID, Name,

  • 0

I have a Category table which has 3 fields i’m interested in:

ID, Name, CategoryType

The root nodes are identified by having CategoryType of 1, subnodes of the root nodes are of type 2 and sub-sub nodes are of type 3.

There’s a 2nd table, CategoryRelationship, of which the two columns that matter are:

CategoryID, ParentCategoryID.

What I would like is a listing of records so that we have the name of each category/subcategory and it’s ID, like below

ID RootName1 ID SubName1 ID Sub-SubName1
ID RootName1 ID SubName1 ID Sub-SubName2
ID RootName1 ID SubName2 ID Sub-SubName1
ID RootName1 ID SubName2 ID Sub-SubName2
ID RootName1 ID SubName2 ID Sub-SubName3
ID RootName2 ID SubName1 ID Sub-SubName1
ID RootName2 ID SubName2
ID RootName2 ID SubName3 

The ID would be of each root and node/subnode etc

I think I’ve got this working – i was just wondering if this is the ‘correct’ way of doing this or if this a better way. This is being done against an MS SQL 2012 express db.

select c.id, 
       c.name, 
       c1.Name, 
       cr1.CategoryID, 
       c2.Name, 
       cr2.CategoryID 
from Category c
left outer join CategoryRelationship cr1 on cr1.CategoryParentID = c.id
left outer join CategoryRelationship cr2 on cr2.CategoryParentID = cr1.CategoryID
inner join Category c1 on c1.ID = cr1.CategoryID
inner join Category c2 on c2.id = cr2.CategoryID
where c.CategoryTypeID = 1
order by c.name, c1.name, c2.name

There’s one more bit to this I need a little help with. There’s a 3rd table that has products in it. Each product has a SubCateoryID which would match up to cr2.CategoryID above. I would like to display the total number of items in the Product table for each cr2 category and i still want to include any categories that have no items in the product table. I’m not sure how to do that last part.

  • 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-08T11:27:52+00:00Added an answer on June 8, 2026 at 11:27 am

    I think I have this:

    select c.ID, c.Name, c1.Name, cr1.CategoryID, c2.Name, cr2.CategoryID, 
    (select count(*) from Product where product.SubcategoryID = cr2.CategoryID and Deleted = 0 and StatusID = 1) 
    from Category c
    left outer join CategoryRelationship cr1 on cr1.CategoryParentID = c.id
    left outer join CategoryRelationship cr2 on cr2.CategoryParentID = cr1.CategoryID
    inner join Category c1 on c1.ID = cr1.CategoryID
    inner join Category c2 on c2.id = cr2.CategoryID
    where c.CategoryTypeID = 1
    order by c.name, c1.name, c2.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table:'Categories' which has two fields:Category_ID and Category. Data in Category_ID field
I have product_details table which belongs to categories. product_details has fields like id, name,
I have a Categories table in which each category has a ParentId that can
Hi have a table named category which has two foreign keys to the table
Let's say I have a table, category , which has 3 columns, id ,
I have the following tables: Category table which has an ID column, a description
I have a table of categories. Each category can either be a root level
I have a table which has no primary key and I can't add one
I have a table which I'm trying to arrange by 2 fields, one of
I have a table, generated from a LINQ query on a datatable, which has

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.