Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hierarchical query - Problem with "Start with" condition

K.BMar 19 2014 — edited Mar 19 2014

Hi folks

I have playing around with the connect by and start with clause and am facing a peculiar problem which I am unable to resolve..

My data set is as follows:

Create table dates_q

(start_date date,

end_date date)

/

REM INSERTING into dates_q

Insert into dates_q ("START_DATE","END_DATE") values (to_date('01-JAN-14','DD-MON-RR'),to_date('10-JAN-14','DD-MON-RR'));

Insert into dates_q ("START_DATE","END_DATE") values (to_date('11-JAN-14','DD-MON-RR'),to_date('20-JAN-14','DD-MON-RR'));

Insert into dates_q ("START_DATE","END_DATE") values (to_date('10-MAR-14','DD-MON-RR'),to_date('20-MAR-14','DD-MON-RR'));

Insert into dates_q ("START_DATE","END_DATE") values (to_date('21-MAR-14','DD-MON-RR'),to_date('31-MAR-14','DD-MON-RR'));

Insert into dates_q ("START_DATE","END_DATE") values (to_date('01-APR-14','DD-MON-RR'),to_date('10-APR-14','DD-MON-RR'));

Now I basically just want to get a hold of hierarchical queries and am working with various syntax...

What I now want is, start with the date of 1st apr as my start date and work backwards to build my "tree". The condition of my tree is between two rows; my start and end date differ by 1 day. If they do not; I do not want those records in my tree.

And using sys_connect_by_path, I want to get the whole path from the root.

So for example,

SELECT a.*,

       sys_connect_by_path(start_date, '|'),

       LEVEL lvl

  FROM dates_q a

CONNECT BY PRIOR end_date = (start_date - 1)

I get the following result

START_DATEEND_DATESYS_CONNECT_BY_PATH(START_DATE,'|')LVL
01.01.201410.01.2014|01-JAN-141
11.01.201420.01.2014|01-JAN-14|11-JAN-142
11.01.201420.01.2014|11-JAN-141
10.03.201420.03.2014|10-MAR-141
21.03.201431.03.2014|10-MAR-14|21-MAR-142
01.04.201410.04.2014|10-MAR-14|21-MAR-14|01-APR-143
21.03.201431.03.2014|21-MAR-141
01.04.201410.04.2014|21-MAR-14|01-APR-142
01.04.201410.04.2014|01-APR-141

But at the moment I have not given any starting point.. Now comes the FUN part..

when I give the starting condition; I am getting only one row :-(

Example

SELECT a.*,

       sys_connect_by_path(start_date, '|'),

       LEVEL lvl

  FROM dates_q a

CONNECT BY PRIOR end_date = (start_date - 1)

START WITH start_date = To_Date('01-apr-2014','dd-mon-yyyy');

Result is

START_DATEEND_DATESYS_CONNECT_BY_PATH(START_DATE,'|')LVL
01.04.201410.04.2014|01-APR-141

Just one row...!!!

I am unable to understand this and work further and require help.

Is it that the tree formation works only in one "direction" and I am going the other way? Don't know what that means but just something that came to mind. :/

Thanks

K

P.S. - Database is 10g R2.

This post has been answered by Frank Kulash on Mar 19 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2014
Added on Mar 19 2014
2 comments
982 views