Oracle query help

I have a dataset that has parents and children that I am calling branches.  Sample dataset:

Branch   TreeId    Name      ParentId
Root        1            GRP A       NULL
Root        2            SET A1      1
Child       6            SET A1B    2
Child       7            SET A1C    2

I then have a TreeId parameter that I need to pull from that level down to the children.  This set will not go higher than a Root record where ParentId IS NULL.  So what I am trying to do is let's say I pass in TreeId 1 in this case I would want the whole set back.   Essentially any ParentId = Null can have additional roots under it.   If I pass treeid = 2 I would want records 2,6 and 7.   If I pass treeid 6 or 7 I would just want that record.

I would want to start with the NULL ParentId if it is null then work down based on the treeid passed in.
sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
You need a Hierarchical query.  There are examples in the online docs:
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Hierarchical-Queries.html#GUID-0118DF1D-B9A9-41EB-8556-C6E7D6A5A84E

I think this should work for you.

Here is my complete test case:
/*
drop table  tab1 purge;
create table tab1(Branch varchar2(10), TreeId number, Name varchar2(10), ParentId number);

insert into tab1 values('Root',1,'GRP A',NULL);
insert into tab1 values('Root',2,'SET A1',1);
insert into tab1 values('Child',6,'SET A1B',2);
insert into tab1 values('Child',7,'SET A1C',2);
commit;
*/

select *
from tab1
connect by prior treeid=parentid
start with treeid = 6
/

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark GeerlingsDatabase AdministratorCommented:
That looks close.  I think you need that last line to be:
start with treeid = 1;
0
PortletPaulfreelancerCommented:
It's more than close...

The question seeks a method that will accept different parameters to produce different results.
The method provided by slightwv is correct; One can use a variety of where clauses and get the expected result of each:

select *
from table1
connect by prior treeid = parentid
start with parentid is null
;

| BRANCH | TREEID |    NAME | PARENTID |
|--------|--------|---------|----------|
|   Root |      1 |   GRP A |   (null) |
|   Root |      2 |  SET A1 |        1 |
|  Child |      6 | SET A1B |        2 |
|  Child |      7 | SET A1C |        2 |


-- pass in TreeId 1, want the whole set back.  
select *
from table1
connect by prior treeid = parentid
start with treeid = 1
;

| BRANCH | TREEID |    NAME | PARENTID |
|--------|--------|---------|----------|
|   Root |      1 |   GRP A |   (null) |
|   Root |      2 |  SET A1 |        1 |
|  Child |      6 | SET A1B |        2 |
|  Child |      7 | SET A1C |        2 |


-- pass treeid = 2, want records 2,6 and 7
select *
from table1
connect by prior treeid = parentid
start with treeid = 2
;

| BRANCH | TREEID |    NAME | PARENTID |
|--------|--------|---------|----------|
|   Root |      2 |  SET A1 |        1 |
|  Child |      6 | SET A1B |        2 |
|  Child |      7 | SET A1C |        2 |

-- pass treeid = 6, want record 6
select *
from table1
connect by prior treeid = parentid
start with treeid = 6
;

| BRANCH | TREEID |    NAME | PARENTID |
|--------|--------|---------|----------|
|  Child |      6 | SET A1B |        2 |

Open in new window

also see http://sqlfiddle.com/#!4/d2a7fa/1
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Mark GeerlingsDatabase AdministratorCommented:
I agree that with this small data set, either of these lines will return the same results (and quickly):
start with parentid is null;
or
start with treeid = 1;

But in most production systems, the performance difference between these two can be *VERY* different!  The "is null" option will force a full-table scan.  If the table has a million records, that will take a while.  The "treeid = 1" option will use an index (assuming the table has an index on this column).  That will be *MUCH* faster if the table has more than a couple thousand rows.
0
slightwv (䄆 Netminder) Commented:
>>I think you need that last line to be:  start with treeid = 1;

Paul is correct.  6 was just my last test case in the original requirement:
If I pass treeid = 2 I would want records 2,6 and 7.   If I pass treeid 6 or 7 I would just want that record.

I didn't post all combinations.  I thought it was sort of a given.

>>these lines will return the same results (and quickly): start with parentid is null;  or start with treeid = 1;

I get no rows is I use 'is null'.
0
PortletPaulfreelancerCommented:
Parentid is null
Will return rows

Treeid is null
Would not return rows from the sample
1
slightwv (䄆 Netminder) Commented:
Sorry.  I didn't read the actual column involved.  :(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.