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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:03:05+00:00 2026-05-31T11:03:05+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

I suppose need to sort the task_seq and return a result like below:

Task Name        || Task_Seq
    Item1               1
    ..Item1.2           1
    ...Item1.2.1        1
    ..Item1.1           2

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

But my result returns duplicated rows:

enter image description here

Anyone can help me correct my 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-05-31T11:03:07+00:00Added an answer on May 31, 2026 at 11:03 am

    I believe that your duplicates are coming from your root/anchor query. You should add the following to that query:

    AND Task_Seq = 0
    

    Basically, you only want the root to be set up as the beginning of the tree. 301|300 should not be picked up until the recursion section (the part after union all)

    If that does not make sense, then I can repaste your query with the modification, but that seemed unnecessary for a small change.

    • 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
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
We have a need to manage a large number (approx 20+) languages for our
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.