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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:24:55+00:00 2026-06-13T00:24:55+00:00

I have a table which maintain version of record with their parent id Eg

  • 0

I have a table which maintain version of record with their parent id
Eg

Table(Id,version,parentId)

I am trying to get all the related record from given Id value

;WITH CTE (Id,ParentId)
AS
(
Select T1.Id,T1.ParentId
From Table T1
Where T1.ID = @Id or T1.ParentId=@Id

Union All

Select T2.Id, T2.ParentId
From Table T2
     Inner Join CTE C on C.Id= T2.ParentId  
)

This works fine for when the main parent id (162527) is provided, it brings me the

Id      VersionNo   ParentId
162527      1.0         0
162530      1.1         162527
162531      1.2         162527
162532      1.2.1       162531
162533      1.2.1.1     162532
162534      1.2.2       162531
200346      1.2.2.1     162534
200354      1.2.2.1.1   200346

But When I pass 200354 as Id, it returns me

200346  1.2.2.1     162534
200354  1.2.2.1.1   200346

But I want the all related record when an id is given.

Any help

Thanks

  • 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-13T00:24:57+00:00Added an answer on June 13, 2026 at 12:24 am
    declare @Table table (id int, parentid int)
    
    declare @id int, @ParentId int
    
    select @id = 162530 
    
    insert into @Table
    select 162527, 0 union all
    select 162530, 162527 union all
    select 162531, 162527 union all
    select 162532, 162531 union all
    select 162533, 162532 union all
    select 162534, 162531 union all
    select 200346, 162534 union all
    select 200354, 200346
    
    ;WITH Parents
    AS
    (
        Select T1.Id, T1.ParentId from @Table T1 where Id = @Id
        Union All
        Select T2.Id, T2.ParentId
        From @Table T2 Inner Join Parents C on C.ParentId = T2.Id
    )
    select @ParentId = P.id
    from Parents as P
    where parentid = 0
    
    ;WITH Children
    AS
    (
        Select T1.Id, T1.ParentId from @Table T1 where Id = @ParentId
        Union All
        Select T2.Id, T2.ParentId
        From @Table T2 Inner Join Children C on C.Id = T2.parentid
    )
    select *
    from Children
    

    Previous one

    ;WITH Children (Id,ParentId)
    AS
    (
        Select T1.Id, T1.ParentId from @Table T1 where Id = @Id
        Union All
        Select T2.Id, T2.ParentId
        From @Table T2 Inner Join Children C on C.ParentId = T2.Id
    ),Parents (Id,ParentId)
    AS
    (
        Select T1.Id, T1.ParentId from @Table T1 where Id = @Id
        Union All
        Select T2.Id, T2.ParentId
        From @Table T2 Inner Join Parents C on C.Id = T2.parentId
    )
    select * from Parents
    union
    select * from Children
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table which has many fields but i want to get count
I have a table which i want to store all the outputs into one
I have a table which stores the data related to posts in this format
I have a GridView containing records from a table which can be deleted or
I have a table with an order column which needs to maintain a contiguous
I am trying to create a table which captures parent child relationships, like a
I have a Competitions results table which holds team member's names and their ranking
I have table which contains double values stored in mysql database ...I need to
I have table in which I am inserting rows for employee but next time
I have one table which contains events and dates, and another which contains the

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.