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

  • Home
  • SEARCH
  • 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 7869589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:22:19+00:00 2026-06-03T01:22:19+00:00

I have two tables, one table is the items container, which holds all data

  • 0

I have two tables, one table is the items container, which holds all data related to the products. The other table is for categories, in which the data is organised using hierarchies.
The goal of the query is to list items that match the category from the selected category down.

Example:

CatID: 3, Parent: Root, Name: Computers  
CatID: 4, Parent: 3, Name: Laptops  
CatID: 5, Parent: 3, Name: Monitors  
CatID: 6, Parent: 3, Name: Printers  
CatID: 7, Parent: 6, Name: Laser  
CatID: 8, Parent: 6, Name: Ink  
CatID: 9, Parent: 6, Name: Multifunction  
CatID: 10, Parent: 6, Name: Copier  

If the CatID selected is 3, all computer products will be displayed, but if the CatID 6 is selected, then only the printers will be displayed (items with CatID 7, 8, 9 and 10)

I’m rather new to hierarchy id and I have little idea on how to aproach this query.

Thanks to all who contribute.

Carlos

Schema of both tables
The idea is to join mz_category to ic_item_cat, so a query could list all items for a given category, but also for all child categories. This is intended for a special “home made” treeview where all categories are displayed according to the hierarchy; when a user clicks a category, it displays all items that belong to that category and all items that belong to the child categories as well. Hope you get the idea.

/****** Object:  Table [dbo].[mz_category]    Script Date: 04/26/2012 19:14:34 ******/
SET ARITHABORT ON
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
SET ARITHABORT ON
GO
/****** Categories Table ******/
CREATE TABLE [dbo].[mz_category](
    [CatNode] [hierarchyid] NOT NULL,
    [CatLevel]  AS ([CatNode].[GetLevel]()),
    [CatID] [int] NOT NULL,
    [CatName] [varchar](80) NOT NULL,
    [SectorId] [varchar](2) NULL,
    [CatIcon] [varchar](255) NULL,
    [oldCat] [varchar](8) NULL,
PRIMARY KEY CLUSTERED 
(
    [CatNode] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
    [CatID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[ic_item_cat]    Script Date: 04/26/2012 19:14:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
/****** Items Table ******/
CREATE TABLE [dbo].[ic_item_cat](
    [item_code] [varchar](25) NOT NULL,
    [item_description] [varchar](80) NULL,
    [item_sector] [varchar](2) NULL,
    [item_line] [varchar](2) NULL,
    [item_reference] [varchar](50) NULL,
    [item_upcean] [varchar](50) NULL,
    [item_category] [int] NULL,
    [item_brand] [varchar](8) NULL,
    [item_cost] [decimal](12, 4) NULL,
    [item_price1] [decimal](12, 4) NULL,
    [item_price2] [decimal](12, 4) NULL,
    [item_price3] [decimal](12, 4) NULL,
    [item_webprice] [decimal](12, 4) NULL,
    [item_dprice1] [decimal](12, 4) NULL,
    [item_dprice2] [decimal](12, 4) NULL,
    [item_dprice3] [decimal](12, 4) NULL,
    [item_lastcost] [decimal](12, 4) NULL,
    [item_lastcostdate] [datetime] NULL,
    [item_lastqtyout] [decimal](12, 4) NULL,
    [item_lastqtyoutdate] [datetime] NULL,
    [item_lastqtyin] [decimal](12, 4) NULL,
    [item_lastqtyindate] [datetime] NULL,
    [item_additionaldesc] [varchar](max) NULL,
    [item_weight] [decimal](12, 4) NULL,
    [item_weight_measure] [varchar](2) NULL,
    [item_width] [decimal](12, 4) NULL,
    [item_width_measure] [varchar](2) NULL,
    [item_length] [decimal](12, 4) NULL,
    [item_length_measure] [varchar](2) NULL,
    [item_height] [decimal](12, 4) NULL,
    [item_height_measure] [varchar](2) NULL,
    [item_whpackdesc] [varchar](80) NULL,
    [item_salespackdesc] [varchar](80) NULL,
    [item_purchpackdesc] [varchar](80) NULL,
    [item_salespackconv] [decimal](12, 4) NULL,
    [item_purchpackconv] [decimal](12, 4) NULL,
    [item_warranty] [varchar](2) NULL,
    [item_delivtime] [varchar](2) NULL,
    [item_rating] [int] NULL,
    [item_vat] [varchar](2) NULL,
    [item_status] [int] NULL,
    [item_avgleadtime] [int] NULL,
    [web_flag] [bit] NULL,
    [partner_id] [varchar](25) NULL,
    [unique_id] [uniqueidentifier] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
  • 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-03T01:22:21+00:00Added an answer on June 3, 2026 at 1:22 am

    Will something like this work?

    DECLARE @CatNode hierarchyid;
    SET @CatNode = (
        SELECT CatNode from mz_category
        WHERE CatID = @CatID
      );
    SELECT * FROM Items
    WHERE CatID IN (
      SELECT  CatID
      FROM    mz_category
      WHERE   CatNode.IsDescendantOf(@CatNode) = 1
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables : one table it matchs and the other is teams.
I have 3 tables which contain different types of data related to each other.
I have two tables, one contains a list of items which is called watch_list
I have two tables, one called cart and one called items. Now I want
In SQL Server 2008 I have two tables. One table for users: id_user -
In a MySQL database I have two tables linked in a join. One table
I have two tables, one is a table of forum threads. It has a
I have two tables, content and images (and a ContentImages table for the one
I have a database that has two tables, one of which contains a foreign
I have a table which has columns of data that contain status. Two example

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.