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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:35:14+00:00 2026-06-15T02:35:14+00:00

I need your help with this query I can’t solve. Here’s the thing, let’s

  • 0

I need your help with this query I can’t solve.
Here’s the thing, let’s say I have a table like this:

Father | Son
1      |   2
1      |   3
1      |   4
3      |   5
3      |   6
2      |   7
4      |   8
5      |   9
6      |   10

So as you can see the table represents a father-son relationship, I need to find all the sons (and the sons of the sons) of one father, for example if Im looking for the sons of number 1 we will get a table like this

Father | son
1      |   2
1      |   3
1      |   4
1      |   5
1      |   6
1      |   7
1      |   8
1      |   9
1      |   10

If I’m lookeing for the sons of the number 3 we will get:

Father | son
3      |   5
3      |   6
3      |   9
3      |   10

okay I think you understood my point. The next problem is that I have to do it for each father in the table. So at the end, the table will be as follows:

Father | son
1      |   2
1      |   3
1      |   4
1      |   5
1      |   6
1      |   7
1      |   8
1      |   9
1      |   10
3      |   5
3      |   6
3      |   9
3      |   10
2      |   7
4      |   8
5      |   9
6      |   10

is pretty similar to go over a tree.
I have been trying to figure out this query for a while with not success. I did a code like this

WITH  son
        AS (
              -- in 
            SELECT  FK_CC_B
            FROM    PG_Rols_CC  
            WHERE   FK_CC_A = @idCC
            UNION ALL
              --recur
            SELECT  t.FK_CC_B
            FROM     PG_Rols_CC AS t
                    JOIN hijos AS a
                      ON t.FK_CC_A = a.FK_CC_B
           )
insert into @TableSons (idSon) SELECT * FROM son

But it doesn’t works for each father at the same time and the resulting table is a little bit different, it only has the son ID. Example

son
3
4
5
6
7

Hope you can help me.

  • 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-15T02:35:16+00:00Added an answer on June 15, 2026 at 2:35 am

    If you want to get the descendents of all fathers, you need to get rid of the WHERE clause in the first part of your Common Table Expression (CTE).

    If you want to output two columns (Father and Descendent), you need to add these columns to both parts of the CTE.

    Like this:

    WITH Son
    AS
    (
        SELECT
            FK_CC_A AS Father,
            FK_CC_B AS Son
        FROM
            PG_Rols_CC
        UNION ALL
        SELECT
            f.Father,
            s.FK_CC_B AS Son
        FROM
            Son f
            INNER JOIN PG_Rols_CC s ON f.Son = s.FK_CC_A
    )
    
    SELECT * FROM Son
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have never tried something like this so i need your help. i have
I need your help to translate a query into pyes. This query is working
i need again your help... I have this php code: <? if(isset($_POST['addnews'])){ $type =
I need your help to solve a silly problem. I have 2 tables in
today i need your help to get an specific sql select query. i have
I’ve been hours trying to build this query and I need your help so
I need your help with this one again. I want to send special characters
I really need your help for this. I am relatively new to programming and
ive being trying to cope with this problem but still. I need your help.
I need some help with a SQL query for SQL Server 2005. Here is

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.