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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:29:47+00:00 2026-06-10T17:29:47+00:00

I have a problem that I’m trying to solve that I think is different

  • 0

I have a problem that I’m trying to solve that I think is different from other SQL adjacency list hierarchy problems I’ve researched here. I’m trying to sort the data such that all children are listed before any of their parents. I already have CTE’s that return all the children of a given ParentID and all the parents of a given ChildID, but those don’t fit my current need.

I have two tables:

CREATE TABLE [dbo].[ItemMaster](
[ItemID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,

CREATE TABLE [dbo].[References](
[RefID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NOT NULL,
[ChildID] [int] NOT NULL,

Foreign key relationships exist between ItemMaster.ItemID to References.ParentID and References.ChildID

Here’s some example data from my tables…

ItemID, Name

  • 1, A1
  • 2, A2
  • 3, A3
  • 4, P1
  • 5, P2
  • 6, P3
  • 7, P4
  • 8, TOP

RefID, ParentID, ChildID

  • 1,1,5
  • 2,2,5
  • 3,2,6
  • 4,3,2
  • 5,7,3
  • 6,8,4
  • 7,8,1
  • 8,8,2
  • 9,8,3
  • 10,8,7

The output I want would list each ItemID and Name with the children listed before any parents. Like this…

ItemID, Name

  • 4, P1
  • 5, P2
  • 6, P3
  • 1, A1
  • 2, A2
  • 3, A3
  • 7, P4
  • 8, Top

Notes:

  • Any ItemID can have 0, 1 or > 1 parents.
    • Those that don’t have any parents would be listed at the top of the output.
  • I don’t have NULL values for the “top most parents” in the reference table.

I hope I’ve given enough detail and explained myself well enough to get some feedback. Any ideas would be greatly appreciated!

  • 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-10T17:29:48+00:00Added an answer on June 10, 2026 at 5:29 pm

    I think the way to achieve this is using a recursive operation and assigning a weight to each node, based on the weight of all its children.

    For example any leaf in this tree will have a weight of zero since it doesn’t have any children. Its immediate parent will be 1, and the parent’s parent 2 and so on. Sorting on the weight should return the result set you need. Here is the recursive function I used to test my theory:

    CREATE FUNCTION [dbo].[GetItemWeight](@ItemID int)
    RETURNS int
    BEGIN
    
    DECLARE @Weight int;
    
    SELECT
      @Weight = COUNT(ParentID) 
    FROM
      [References]
    WHERE
      ChildID = @ItemID;
    
    SELECT
      @Weight = ISNULL(@Weight, 0) + SUM(dbo.GetItemWeight(ChildID))
    FROM
      [References]
    WHERE
      ParentID = @ItemID;
    
    RETURN ISNULL(@Weight, 0);
    END
    

    This returns the result set you posted:

    SELECT ItemID, Name FROM ItemMaster ORDER BY dbo.GetItemWeight(ItemID);
    

    Best of luck 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem that i can't solve ! (sqlite3, but i think it
I have problem that im trying to get solve for like week. My goal
I have a problem that I have been trying to find a solution for
I have a problem that I have not managed to solve. I developed a
I have a problem that Google Map API is inheriting the style from my
I have a problem that I would like have solved via a SQL query.
I have this problem that I cannot fix. From my @Controller , i can
i have this problem that i can't solve for days now...here is my code.
I have problem that I don't seem to be able to solve. I have
Hi I have problem that I can't solve alone since damn debugging doesn't work

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.