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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:02:37+00:00 2026-05-19T04:02:37+00:00

I am creating a state chart of sorts with the data being stored in

  • 0

I am creating a state chart of sorts with the data being stored in a simple self referencing table (JobPath)

JobId - ParentJobId

I was using a standard SQL CTE to get the data out which was working perfectly until I ended up with the following data

JobId - ParentId
  1        2
  2        3
  3        4
  4        2

Now as you can see Job 4 links to Job 2 which goes to Job 3 and then to Job 4 and so on.

Is there any way I can tell my query not to pull out data it already has?


Here is my current query

WITH JobPathTemp (JobId, ParentId, Level)
AS
(
-- Anchor member definition
    SELECT j.JobId, jp.ParentJobId, 1 AS Level
    FROM Job AS j
    LEFT OUTER JOIN dbo.JobPath AS jp
        ON j.JobId = jp.JobId
    where j.JobId=1516
    UNION ALL
-- Recursive member definition
    SELECT  j.JobId, jp.ParentJobId, Level + 1
    FROM dbo.Job as j
    INNER JOIN dbo.JobPath AS jp
        ON j.JobId = jp.JobId
    INNER JOIN JobPathTemp AS jpt
        ON jpt.ParentId = jp.JobId
      WHERE jp.ParentJobId <> jpt.JobId 
)

— Statement that executes the CTE

SELECT * FROM JobPathTemp
  • 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-19T04:02:37+00:00Added an answer on May 19, 2026 at 4:02 am

    If you are not dealing with a large number of entries, the following solution might be suitable. The idea is to build the complete “id path” for each row and make sure the “current id” (in the recursive part) is not already in the path being processed:

    (I removed the join to jobpath for testing purposes but the basic pattern should be the same)

    WITH JobPathTemp (JobId, ParentId, Level, id_path)
    AS
    (
      SELECT jobid, 
             parentid, 
             1 as level, 
             '|' + cast(jobid as varchar(max)) as id_path
      FROM job
      WHERE jobid = 1
    
      UNION ALL
    
      SELECT  j.JobId, 
              j.parentid, 
              Level + 1, 
              jpt.id_path + '|' + cast(j.jobid as varchar(max))
      FROM Job as j
        INNER JOIN JobPathTemp AS jpt ON j.jobid = jpt.parentid 
                                         AND charindex('|' + cast(j.jobid as varchar), jpt.id_path) = 0
    )
    SELECT *
    FROM JobPathTemp
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below mentioned stored procedure is giving error while creating Msg 156, Level 15, State
I am creating a Serializable object descriptor that will store important state data for
I am creating stacked bar charts on subsets of data using a loop. I'm
I have three entities: Country, State and City with the following relationships: When creating
I have a C++ state machine implemented using the State design pattern. Each state
Could anyone provide me with some assistance in creating a minimum example of using
I'm experimenting with using a finite state machine as a model for managing the
Apples docs state: You should avoid creating UIImage objects that are greater than 1024
When creating classes for immutable objects immutable meaning that state of instances can not
I am creating a data integration tool in C# that will move data from

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.