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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:45:26+00:00 2026-06-01T02:45:26+00:00

I need to manage hierarchy data storing in my database. But I have a

  • 0

I need to manage hierarchy data storing in my database. But I have a problem now. Please see my example

I have a table called COMMON.TASK_REL

enter image description here

My second table is called Common. task

enter image description here

How can i return the parent task ID ? For example When i select C_Task_ID 307 then it will return the parent ID which is 304

Here is my query

  --Common task SQL modify --
WITH ctLevel
AS
(
SELECT
   C_TASK_ID AS Child
  ,P_Task_ID AS Parent
  ,common_task.TASK_SEQ AS taskOrder
  ,1 AS [Level]
  ,CAST(C_TASK_ID AS VARCHAR(MAX)) AS [Order]
  ,CAST (Replicate('.', 1) + common_task.TASK_NAME AS VARCHAR(25)) AS [Task_Name]
FROM   
       [COMMON.TASK_REL] as common_task_rel, 
       [COMMON.TASK] as common_task
WHERE common_task_rel.C_TASK_ID = common_task.TASK_ID
    and common_task.[TASK_TYPE] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT' 
    AND common_task.[STATUS] <> 'D'
UNION ALL

SELECT 
   C_TASK_ID AS Child
  ,P_Task_ID AS Parent
  ,common_task.TASK_SEQ AS taskOrder
  ,[Level] + 1 AS [Level]
  ,[Order] + '.' + CAST(C_TASK_ID AS VARCHAR(MAX)) AS [Order]
  ,CAST (Replicate('.', [Level] + 1) + common_task.TASK_NAME AS VARCHAR(25)) AS [Task_Name]
FROM   [COMMON.TASK_REL] as common_task_rel
    INNER JOIN ctLevel
       ON ( P_Task_ID = Child ) , [COMMON.TASK] as common_task
WHERE common_task_rel.C_TASK_ID = common_task.TASK_ID
    and common_task.[TASK_TYPE] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT'
    AND common_task.[STATUS] <> 'D'
)

-- Viewing Data
SELECT Child ,Parent ,taskOrder,Level,[Order],Task_Name
FROM   ctLevel  
GROUP BY Child ,Parent ,taskOrder,Level,[Order],Task_Name
order by [Order];
GO

Please help me modify the query. 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-01T02:45:27+00:00Added an answer on June 1, 2026 at 2:45 am

    Your query is quite complex so I will simplify some suggestions for you.

    First some test data:

    DECLARE @TASK_REL TABLE
        (
            ID INT,
            P_TASK_ID INT,
            C_TASK_ID INT
        )
    INSERT INTO @TASK_REL
    VALUES
        (21,NULL,300),
        (22,300,301),
        (23,300,302),
        (24,300,303),
        (25,NULL,304),
        (26,304,305),
        (27,304,306),
        (28,304,307)
    
    DECLARE @Common TABLE
        (
            TASK_ID INT,
            TASK_Name VARCHAR(100),
            TASK_SEQ INT,
            MODULE_CODE VARCHAR(100),
            STATUS VARCHAR(5),
            TASK_TYPE VARCHAR(5)
        )
    
    INSERT INTO @Common
    VALUES
        (300,'Item1',0,'LWRPT','A','B'),
        (301,'Item 1.1',1,'LWRPT','A','B'),
        (302,'Item 1.2',2,'LWRPT','A','B'),
        (303,'Item 1,3',3,'LWRPT','A','B'),
        (304,'Item 2',0,'LWRPT','A','B'),
        (305,'Item 2.1',1,'LWRPT','A','B'),
        (306,'Item 2.2',2,'LWRPT','A','B'),
        (307,'Item 2.3',3,'LWRPT','A','B')
    

    If you know that you just have one parent per child you can easily do this:

    SELECT
        Common.TASK_Name,
        CommonParent.TASK_Name
    FROM
        @Common AS Common
        JOIN @TASK_REL AS TASK_REL
            ON Common.TASK_ID=TASK_REL.C_TASK_ID
        JOIN @Common AS CommonParent
            ON ISNULL(TASK_REL.P_TASK_ID,TASK_REL.C_TASK_ID)=CommonParent.TASK_ID
    

    Because you did a recursive cte on your tables. I think that you might want something like this:

    ;WITH CTE(C_TASK_ID,P_TASK_ID,TopParent)
    AS
    (
        SELECT
            TASK_REL.C_TASK_ID,
            TASK_REL.P_TASK_ID,
            C_TASK_ID AS TopParent
        FROM
            @TASK_REL AS TASK_REL
        WHERE
            TASK_REL.P_TASK_ID IS NULL
        UNION ALL
        SELECT
            TASK_REL.C_TASK_ID,
            TASK_REL.P_TASK_ID,
            CTE.TopParent
        FROM
            @TASK_REL AS TASK_REL
        JOIN CTE
            ON CTE.C_TASK_ID=TASK_REL.P_TASK_ID
    )
    SELECT
        Common.TASK_Name,
        CommonParent.TASK_Name
    FROM
        CTE
        JOIN @Common AS Common
            ON CTE.C_TASK_ID=Common.TASK_ID
        JOIN @Common AS CommonParent
            ON CTE.TopParent=CommonParent.TASK_ID
    

    You had a lot of joins in the recursive parts. I think you should one include the ones that are depended on the recursion and then join with it at the end.

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

Sidebar

Related Questions

I need to manage hierarchy data storing in my database. But I have a
I need to manage hierarchy data storing in my database. But now I am
i have got 24 buttons in my project.I need to manage them but I
We have a need to manage a large number (approx 20+) languages for our
i need to connect to another server we manage and have it's results (in
I need to manage the trace files for a database on Sql Server 2005
I have handles of different types inside a hierarchy. class Handle { common data
I need to manage (parse and build) XML in my c++ application but I
For every IBOutlet that you need to manage you need to have a pointer,
I have a situation which involves several threads simultaneously populating a database with data

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.