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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:53:34+00:00 2026-06-09T14:53:34+00:00

I am wondering what an efficient way to crawl up a company structure would

  • 0

I am wondering what an efficient way to crawl up a company structure would be by using the parent ID. Essentially what I am trying to do is find the first ContactID that exists, as these contacts are inherited down to the child locations.

For Example:

Test Account
-North
--Michigan - Bob
---Detroit
----Algoma
-South
-East
-West

So Bob would be inherited to Detroit and Algoma. Each level has an ID and a Parent ID which of course matches the ID of the Parent. If given the ChildID(Algoma) how would I crawl up and check each level above Algoma for a contact until I get to Michigan? Each level only has 1 contact. There is no set structure so the contact could be at the child location, or it could be 20 levels up before you get to the contact, but it is always the first 1 going up the tree structurally. Any ideas are appriciated.

TABLE:

ID ParentID LevelName ContactID
1     NULL   TestCo.    NULL
2     3      Algoma     NULL
3     4      Detroit    NULL
4     5      Michigan   2
5     1      North      3
6     1      South      1
7     1      East       3
8     1      West       NULL

Pass in @ID = 2, it would return ContactID = 2. This is not a static structure for every company, there can be many levels deeper or shorter.

  • 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-09T14:53:36+00:00Added an answer on June 9, 2026 at 2:53 pm

    Recursive queries to the rescue!
    There’s also a parent-first version:

    WITH Parent (id, contactId) as (SELECT id, contactId
                                    FROM Company
                                    WHERE parentId IS NULL
                                    UNION ALL
                                    SELECT Company.id, 
                                           COALESCE(Company.contactId, 
                                                    Parent.contactId)
                                    FROM Company
                                    JOIN Parent
                                    ON Parent.id = Company.parentId)
    
    SELECT *
    FROM Parent
    WHERE id = 2;
    

    (have a working SQLFiddle example.)

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

Sidebar

Related Questions

I'm wondering if there's a super-efficient way of confirming that an Image object references
Wondering what would be the most efficient way to do the following. Game play.
I was simply wondering what would be the simplest and most efficient way of
I am wondering what is the fastest, most efficient way (using JAVA) to search
I'm developing my first EclipseLink J2SE project, and wondering if there's an efficient way
I was wondering what would be the most efficient way to make a map
I'm wondering how exactly to perform a 3-way join using the following structure (much
I am trying to figure out the most efficient way to find the degree
I am wondering which is more efficient, to store temporary data (related to that
Wondering what the best / good way of doing this would be in jQuery.

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.