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_DATE | END_DATE | SYS_CONNECT_BY_PATH(START_DATE,'|') | LVL |
| 01.01.2014 | 10.01.2014 | |01-JAN-14 | 1 |
| 11.01.2014 | 20.01.2014 | |01-JAN-14|11-JAN-14 | 2 |
| 11.01.2014 | 20.01.2014 | |11-JAN-14 | 1 |
| 10.03.2014 | 20.03.2014 | |10-MAR-14 | 1 |
| 21.03.2014 | 31.03.2014 | |10-MAR-14|21-MAR-14 | 2 |
| 01.04.2014 | 10.04.2014 | |10-MAR-14|21-MAR-14|01-APR-14 | 3 |
| 21.03.2014 | 31.03.2014 | |21-MAR-14 | 1 |
| 01.04.2014 | 10.04.2014 | |21-MAR-14|01-APR-14 | 2 |
| 01.04.2014 | 10.04.2014 | |01-APR-14 | 1 |
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_DATE | END_DATE | SYS_CONNECT_BY_PATH(START_DATE,'|') | LVL |
| 01.04.2014 | 10.04.2014 | |01-APR-14 | 1 |
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.