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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:33:36+00:00 2026-05-20T17:33:36+00:00

I have a table with navigation that joins back on its self using ParentId.

  • 0

I have a table with navigation that joins back on its self using ParentId. I am trying to calculate how many descendents each record has, I know that I need to increment a counter in the recursion, I’m just not sure how to go about it!

Any help would be greatly appreciated!

CREATE TABLE [dbo].[Navigation](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [AnchorText] [varchar](50) NOT NULL,
    [ParentID] [int] NULL)

insert into Navigation
select 'Level 1', null
union
select 'Level 2', 1
union
select 'Level 3', 2
union
select 'Level 4', 3


WITH NavigationCTE (Id, AnchorText, ParentId, NumberofDescendants) as
(
      Select Id, AnchorText, ParentId, 'Number of Descendants Here' as NumberofDescendants
      from dbo.Navigation nav

      union ALL 

      select nav.Id, nav.AnchorText, nav.ParentId,  'Number of Descendants Here' as NumberofDescendants
      from dbo.Navigation nav

      join Navigation ON nav.ParentId = nav.Id
)


SELECT * FROM NavigationCTE

EDIT Added Level and Incremented in recursion:

WITH NavigationCTE (Id, AnchorText, ParentId, Level) as
(
      Select nav.Id, nav.AnchorText, nav.ParentId, 0 as Level
      from dbo.Navigation AS nav

      UNION ALL 

      select nav.Id, nav.AnchorText, nav.ParentId, Level + 1
      from dbo.Navigation AS nav

      join Navigation AS nav2 ON nav.ParentId = nav2.Id
)


SELECT * FROM NavigationCTE
  • 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-05-20T17:33:36+00:00Added an answer on May 20, 2026 at 5:33 pm

    Common Table Expressions provide the kind of recursive functionality you need. Study the creation and use of the Level field in the article’s sample query. It performs exactly the kind of incrementing you want to achieve, starting with 0 in the anchor query.

    Working query based on provided sample:

    WITH NavigationCTE  AS
    (   
        SELECT navA.[Id], navA.ParentId, 0 AS depth_lvl
        FROM Navigation as navA
    
        UNION ALL
    
        SELECT navB.Id, navB.ParentId, depth_lvl + 1
        FROM Navigation AS navB
        JOIN NavigationCTE AS nav_cte_a
            --ON navB.ParentId = nav_cte_a.Id
            ON nav_cte_a.ParentId = navB.Id
    )
    SELECT Id, ParentId, coalesce(max(depth_lvl),0)
    FROM NavigationCTE
    GROUP BY Id, ParentId
    ORDER BY Id, ParentId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables that have a relation ship to each other Table A
I'm trying to build a navigation system using categories table with hierarchies. Normally, the
While trying to use LINQ to SQL I encountered several problems. I have table
I have a table in a SQL Server 2005 database with a trigger that
I have a table in MySQL dB that records when a person clicks on
I have Tasks table with two navigation properties - VersionReported and VersionResolved, both stored
I am currently trying to build a navigation that is semi built by the
I have table inside a div tab. The table has 40 rows in it
I have table with 50 entries (users with such details like Name Surname Location
I have table rows of data in html being filled from a CGI application.

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.