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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:20:12+00:00 2026-06-18T22:20:12+00:00

I came across a query during work and could not figure out how exactly

  • 0

I came across a query during work and could not figure out how exactly it works. What the query does is look for all the parents to a person that are its parent today.

Now the trick here is that each parent child relationship has a duration for which it is valid.

Take this data set as a reference:

GrandParent is parent of Father from 01-01-2012 to 02-02-2015

Father is parent of Child from 01-01-2012 to 02-02-2011

Child is just the lowest level person

NewFather is parent of Child from 01-01-2012 to 02-02-2014

now the list of parents valid today for Child should consist only of NewFather

to get the list, previously we used this SQL:

SELECT connect_by_root per_id2 AS per_id2,
       per_id1,
       LEVEL                   AS per_level,
       n.entity_name
FROM   ci_per_per pp,
       ci_per_name N
WHERE  N.per_id = per_id1
       AND start_dt <= SYSDATE
       AND ( end_dt IS NULL
              OR end_dt >= SYSDATE )
START WITH per_id2 = :personID
CONNECT BY NOCYCLE PRIOR per_id1 = per_id2;

where personID is a binded variable

this query did not work because the where clause behavior is such that it first gets all the records and then checks for the non-join conditions (checks for start date and end date). This results in it giving list of parents as NewFather, GrandParent which is completely WRONG!

Thus, the query was changed to the Following:

SELECT connect_by_root per_id2 AS per_id2,
       per_id1,
       LEVEL                   AS per_level,
       n.entity_name
FROM   ci_per_per pp,
       ci_per_name N
WHERE  N.per_id = per_id1
       AND start_dt <= SYSDATE
       AND ( end_dt IS NULL
              OR end_dt >= SYSDATE )
START WITH per_id2 = (SELECT per_id
                      FROM   ci_acct_per
                      WHERE  per_id = :personID
                             AND pp.start_dt <= SYSDATE
                             AND ( pp.end_dt IS NULL
                                    OR pp.end_dt >= SYSDATE ))
CONNECT BY NOCYCLE PRIOR per_id1 = per_id2;

Now what I don’t understand is:

how can a where condition in the start with clause affect the behavior of the query in such a manner?

Another thing that I dislike about this query is that it uses a completely unrelated table named ci_acct_per which simply has a column of per_id in it for each person in ci_per_per.

Can we do better? Is a cleaner approach available for the fixing the original query?

UPDATE

This query works only if traveling higher in the hierarchy and not if we are looking for children. However, this query never looks for children and is not supposed to.

  • 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-18T22:20:14+00:00Added an answer on June 18, 2026 at 10:20 pm

    I’m not sure that I understand you right, but why not:

    SELECT connect_by_root per_id2 AS per_id2,
           pp.per_id1,
           LEVEL                   AS per_level,
           n.entity_name
    FROM   (select * 
            from ci_per_per
           where start_dt <= SYSDATE
           AND ( end_dt IS NULL
                  OR end_dt >= SYSDATE )) pp join
           ci_per_name N on N.per_id = pp.per_id1         
    START WITH per_id2 = 1
    CONNECT BY NOCYCLE PRIOR pp.per_id1 = pp.per_id2;
    

    Here is a sqlfiddle demo


    Update Thanks to @user1395 example:

    It’s hard to explain how come the strange query works because it doesn’t…

    What really happens is that the START WITH clause uses per_id2 which is the “father of” column, so if there are more than one (one isn’t relevant to sysdate) you still need not to start with it.
    In other words it doesn’t start from “child” but from “child” fathers – “father” and “newfather”.

    So, either use @user1395 suggestion of having date logic in both the connect by clause to stop when a father isn’t relevant, and start with clause to make only the relevant father available, or remove all unrelevant fathers on the first place (as in my former suggestion) or “start with” the “child” and not its fathers:

    select * from (
    SELECT connect_by_root per_id1 AS per_id2,
           per_id1,
           LEVEL                   AS per_level,
           n.entity_name
    FROM   ci_per_per pp,
           ci_per_name N
    WHERE  N.per_id = per_id1       
    START WITH per_id1 = 1
    CONNECT BY NOCYCLE PRIOR per_id1 = per_id2 AND start_dt <= SYSDATE
           AND ( end_dt IS NULL
                  OR end_dt >= SYSDATE ))
    where per_id1 <> per_id2;
    

    Another sqlfiddle demo

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

Sidebar

Related Questions

I just came across a query that does an inner join on a non-distinct
I'm trying out StreamInsight and I came across a problem with a query I
It could be a stupid query, but i came across of this query multiple
I came across this issue, and I just can't figure out what to do,
Today I came across the need to write a CAML query that uses WHERE
I just came across jLinq , a JS library to query data (e.g. arrays)
I came across writing the query in differnt ways like shown below Type-I SELECT
Just came across a place where I'd like to use generics and I'm not
During development I came across a strange scenario using DD with Macedonian_FYROM_90_CI_AS Collation. declare
While researching Oracle Analytics, I came across this query: select an_id, a_date, min(a_date) over

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.