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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:28:06+00:00 2026-06-15T22:28:06+00:00

I have some hierarchical categories in SQL Server, defined as such: CREATE TABLE [dbo].[CategoryDesc]

  • 0

I have some hierarchical categories in SQL Server, defined as such:

CREATE TABLE [dbo].[CategoryDesc] (
    [CategoryId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](255) NOT NULL
)

CREATE TABLE [dbo].[CategoryRelationship] (
    [RelationshipId] [int] IDENTITY(1,1) NOT NULL,
    [CategoryId] [int] NOT NULL,
    [ParentId] [int] NULL
)

CREATE TABLE [dbo].[Machine] (
    [MachineId] [int] IDENTITY(1,1) NOT NULL,
    [Make] [varchar](50) NOT NULL,
    [Model] [varchar](255) NOT NULL,
    [CreateDate] [datetime] NOT NULL,
    [CategoryId] [int] NOT NULL
)

NB: Primary keys, relationships and some fields omitted for brevity.

I already use CTE’s to be able to produce a full tree, an excerpt of which outputs:

Foundry/Forging
Foundry/Forging > Core Equipment
Foundry/Forging > Furnaces
Foundry/Forging > Furnaces > Induction
Glass
Glass > Bevelling
Glass > Cutting
Glass > Cutting > Laminated Glass
Glass > Cutting > Manual
Glass > Cutting > Shape
Glass > Cutting > Straight
Glass > Decorating
Glass > Drilling
Glass > Drilling > Horizontal
Glass > Drilling > Vertical
etc

I need a query that given the top-level categoryId, returns the latest machine added (based on CreateDate) regardless of which child category and at which level it was added.

The problem I’m having is that machines may be added to either 2nd or 3rd level categories (although never the top level) and CTE’s don’t allow left joins on the recursion part.

Thanks in advance for any help given. 🙂

  • 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-15T22:28:07+00:00Added an answer on June 15, 2026 at 10:28 pm

    I have simplified your schema to:

    create table cat( id int, name varchar(100) );
    create table tree ( id int , parent_id int);
    create table item( id int, name varchar(100), cat_id int);
    
    insert into cat values 
    ( 1, 'HW'),
    ( 2, 'HW-PC'),
    ( 3, 'HW-M' ),
    ( 4, 'Soft'),
    (5,'HW-M-N');
    
    insert into tree values
    (2,1),
    (3,1),
    (5,3);
    
    insert into item values 
    ( 1, 'A', 2),
    ( 2, 'B', 3);
    

    The query you request, for root category = 1 is:

    with child_id as
    ( 
      select parent_id as id
          from tree
          where parent_id = 1                         --here cat = 1
       union all
       select tree.id
       from child_id  c
       inner join tree on tree.parent_id = c.id
    ),
    productes as (
       select item.*,
        row_number() over (order by item.id ) as rn  --set your order criteria
       from item 
       inner join child_id c on c.id = item.cat_id
    )
    select * 
     from productes
    where rn = 1
    

    Results:

    | ID | NAME | CAT_ID | RN |
    ---------------------------
    |  1 |    A |      2 |  1 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an algorithm to layout some hierarchical data but have never
I guess should be some visualization tools (MS SQL Server) to represent some hierarchical
I have some hierarchical data that I need display as a table, can I
I have some table: Title 1 | Title 2 | Title 3 | --------------------------------------------------
I'm working on a project where I have some hierarchical data that I want
I'm designing a Database and I have some doubts on using Hierarchical datamodels in
I have some hierarchical data in a Rails 3 applications. It allows the user
I have a hierarchical TreeView object with some structure: Main level second level 1
I have a multi module maven project and the directory structure is hierarchical. Some
I'm trying to create a SQL statement which will recreate the hierarchical container/folder/test structure

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.