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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:20:09+00:00 2026-06-05T11:20:09+00:00

I have a table task_dependency looks like following task_id depend_task_id ————————- 1 NULL 2

  • 0

I have a table task_dependency looks like following

task_id   depend_task_id
-------------------------
1             NULL
2             NULL
3             1
4             2
4             1
5             4
5             3

I try to run a query to get a distinct task dependency list on each task (include task itself).

Expected output (task list order doesn’t matter):

task_id depend_task_list
-----------------------------------------------
1       1;
2       2;
3       1;3
4       1;2;4
5       1;2;3;4;5

What I have so far

DECLARE @t TABLE (task_id INT, depend_task_id INT)

INSERT INTO @t VALUES (1, NULL),(2,NULL),(3,1),(4,2),(4,1),(5,4),(5,3)

;WITH AllDependency (task_id,depend_task_list)
AS(  
   SELECT 
     task_id,
     depend_task_list=CAST(task_id AS VARCHAR(4000))
   FROM @t
   WHERE depend_task_id IS NULL
 UNION ALL
   SELECT 
     t.task_id,
     depend_task_list= CAST(CAST(t.task_id AS VARCHAR(10)) + ';' + depend_task_list AS VARCHAR(4000))
   FROM @t t
   INNER JOIN AllDependency  AS d
        ON d.task_id = t.depend_task_id
) 
SELECT 
    task_id,
    (SELECT depend_task_list + ';' FROM AllDependency d2 WHERE d.task_id = d2.task_id FOR XML PATH('')) AS depend_task_list
FROM AllDependency d
GROUP BY task_id
ORDER BY task_id

Current output:

task_id depend_task_list
-----------------------------------------
1           1;
2           2;
3           3;1;
4           4;2;4;1;
5           5;4;2;5;4;1;5;3;1;

Question:
How can I get a distinct task dependency list without using Cursor?

SQLFiddle Link http://sqlfiddle.com/#!3/4c034/2

Thanks in advance for any assistance.

  • 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-05T11:20:10+00:00Added an answer on June 5, 2026 at 11:20 am

    Ok, try this query and see if it helps:

    DECLARE @t TABLE (task_id INT, depend_task_id INT)
    
    INSERT INTO @t VALUES (1, NULL),(2,NULL),(3,1),(4,2),(4,1),(5,4),(5,3)
    
    ;WITH CTE AS
    (
        SELECT *
        FROM @t
        UNION ALL
        SELECT A.task_id, B.depend_task_id
        FROM CTE A
        INNER JOIN @t B
        ON A.depend_task_id = B.task_id
        --WHERE B.depend_task_id IS NOT NULL
    ), CTE2 AS 
    (
        SELECT DISTINCT task_id, ISNULL(depend_task_id,task_id) depend_task_id
        FROM CTE
    )
    SELECT  t1.task_id, 
            STUFF( (SELECT ';' + CAST(t2.depend_task_id AS VARCHAR(5))
                    FROM CTE2 t2
                    WHERE t2.task_id = t1.task_id
                    ORDER BY depend_task_id
                    FOR XML PATH('')),1,1,'') AS depend_task_list
    FROM CTE2 t1
    GROUP BY task_id
    

    Here is a test on sqlfiddle.

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

Sidebar

Related Questions

I have a sample table (table_name: track_task) like below: task_id stage log_date ---------------------------------------- 3
Okay I have a table that looks as following: id | status(bool) | devicename(text)
I have a table with details on personnel. I would like to create a
I have table with the folowing columns: row_id, customer_id, worker_id, vehicle_id, task_id, subcontractor_id. I
I have table with data about tasks like Id, Name, Date, WorkerId, VehicleId (and
I have a table in my database called Task. Task has the following fields:
I have a task which has to be run everyday. I will get just
I have two tables: CREATE TABLE [dbo].[Task]( [SysTask] [int] IDENTITY(1,1) NOT NULL, [TaskStatus] [int]
I have following inheritance hierarchy: Task | SpecificTask | VerySpecificTask And I'd like to
I have a table of 3-4 columns, the central one being task names that

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.