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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:40:34+00:00 2026-06-02T00:40:34+00:00

Here is the example structure of the table: ID Name ParentID ———————– 1 Ancestor

  • 0

Here is the example structure of the table:

ID    Name     ParentID
-----------------------
1     Ancestor      NULL
2     GrandFather   1
3     GrandMother   1
4     Child         3

I’m trying to write a query that would return

ID     Name        Family
----------------------------
 1     Ancestor 
 2     GrandFather Ancestor
 3     GrandMother Ancestor
 4     Child       Ancestor^GrandMother

The tricky part is that I want to show the family of all rows and in a top-down order.

If anyone can point me in the right direction, it would be appreciated 🙂

EDIT :: This is the real query, but it follows the same idea. it returns an error on line : marketparent.family + ‘^’+ t2.marketGroupName because it cant find marketparent

WITH marketparent ( marketGroupID,parentGroupID, marketGroupName,family)
AS
(
SELECT marketGroupID,
       parentGroupID,
       marketGroupName,
       '' as family 
 FROM EVE.dbo.invMarketGroups
 WHERE parentGroupID IS NULL
UNION ALL

    SELECT t2.parentGroupID,
     t2.marketGroupID,
     t2.marketGroupName,
     marketparent.family + '^'+ t2.marketGroupName
     FROM EVE.dbo.invMarketGroups as t2
     INNER JOIN marketparent as mp
     ON mp.marketGroupID = t2.parentGroupID
)

-- Statement using the CTE

SELECT TOP 10 *
FROM marketparent;
  • 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-02T00:40:35+00:00Added an answer on June 2, 2026 at 12:40 am

    You did not specify your DBMS, so I’m assuming PostgreSQL

    WITH RECURSIVE fam_tree (id, name, parent, family) as 
    (
      SELECT id, 
             name, 
             parentid, 
             ''::text as family
      FROM the_unknown_table
      WHERE parent IS NULL
    
      UNION ALL
    
      SELECT t2.id, 
             t2.name, 
             t2.parentid, 
             fam_tree.family || '^' || t2.name
      FROM the_unknown_table t2 
         INNER JOIN fam_tree ON fam_tree.id = t2.parentid
    )
    SELECT *
    FROM fam_tree;
    

    This is standard SQL (except for the ::text typecast) that should work with very few changes on most modern DBMS.

    Edit:

    For SQL Server you would need to replace the standard concatention character with Microsoft’s non-standar + (and you need to remove the recursive keyword which is required by the standard but for some strange reason rejected by SQL Server)

    WITH fam_tree (id, name, parent, family) as 
    (
      SELECT id, 
             name, 
             parentid, 
             '' as family
      FROM the_unknown_table
      WHERE parent IS NULL
    
      UNION ALL
    
      SELECT t2.id, 
             t2.name, 
             t2.parentid, 
             fam_tree.family + '^' + t2.name
      FROM the_unknown_table t2 
         INNER JOIN fam_tree ON fam_tree.id = t2.parentid
    )
    SELECT *
    FROM fam_tree;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the structure of my Categories table Categories Table -------------------------------- ID Name ParentID
For example I have a file name call A.java. Here its structure: public class
Here is the example with comments: class Program { // first version of structure
I have a table structure like the following: table a: id int name varchar
So firstly here is the relevant table structure: TUBE_VIDEOS ------ id video_name TAGS ----
in my database one table is there with say name as tableA.the structure of
I am trying to write a SQL query that returns rows from a table
I'm trying to write a bash script which will, given the name of a
I can't use the example here because it's specific to the structure the asker
I have a mysql table with contents the structure is here: I want to

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.