Tricky Hierarchical Query

2001-08-31 Thread Walter K

Hi,

I am stumped on how to do a particular hierarchical
query. The query needs to be written entirely in SQL.

I have a table (ACCOUNT) with 3 columns of interest in
it, ACCOUNT_ID, ROOT_ID, PARENT_ID. Every row
represents an account. If an account is the root
(top-most), then the ROOT_ID and PARENT_ID columns are
null. If an account is a parent, then the ROOT_ID is
populated but the PARENT_ID is null. Accounts can be
nested multiple levels deep.

I need to find all of the accounts that belong to the
root of the given account but not include any accounts
that are children of the given account. Does this make
sense?

I.e.  5
  6   7
   8 9  10
11   1213  14

I'm not sure if my tree diagram will be dispalyed
properly after I email this but in essence if 8 is
the given account number, I want everything returned
except for 8, 11 and 12.

Thanks in advance!
-w




__
Do You Yahoo!?
Get email alerts  NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Walter K
  INET: [EMAIL PROTECTED]

Fat City Network Services-- (858) 538-5051  FAX: (858) 538-5051
San Diego, California-- Public Internet access / Mailing Lists

To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).



RE: Tricky Hierarchical Query

2001-08-31 Thread John Lewis

Oracle has a very good section on prior and connect by.
These combined level let you walk a tree - so to speak.

I use them all the time. Look up the key words. You'll find it.

-Original Message-
Sent: Friday, August 31, 2001 12:07 PM
To: Multiple recipients of list ORACLE-L


Hi,

I am stumped on how to do a particular hierarchical
query. The query needs to be written entirely in SQL.

I have a table (ACCOUNT) with 3 columns of interest in
it, ACCOUNT_ID, ROOT_ID, PARENT_ID. Every row
represents an account. If an account is the root
(top-most), then the ROOT_ID and PARENT_ID columns are
null. If an account is a parent, then the ROOT_ID is
populated but the PARENT_ID is null. Accounts can be
nested multiple levels deep.

I need to find all of the accounts that belong to the
root of the given account but not include any accounts
that are children of the given account. Does this make
sense?

I.e.  5
  6   7
   8 9  10
11   1213  14

I'm not sure if my tree diagram will be dispalyed
properly after I email this but in essence if 8 is
the given account number, I want everything returned
except for 8, 11 and 12.

Thanks in advance!
-w




__
Do You Yahoo!?
Get email alerts  NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Walter K
  INET: [EMAIL PROTECTED]

Fat City Network Services-- (858) 538-5051  FAX: (858) 538-5051
San Diego, California-- Public Internet access / Mailing Lists

To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: John Lewis
  INET: [EMAIL PROTECTED]

Fat City Network Services-- (858) 538-5051  FAX: (858) 538-5051
San Diego, California-- Public Internet access / Mailing Lists

To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).



RE: Tricky Hierarchical Query

2001-08-31 Thread Sunil_Nookala

Hi,
I've been through with that lately, modify this to see how it helps.

SELECT LPAD(' ',5*(LEVEL-1)) ||name||' empno:'||emp_no   ||'   '||'works
for:'||functional_mgr_name
FROM HRTABLE
where functional_mgr_badge =emp_no
START WITH emp_no= emp_no
CONNECT BY PRIOR emp_no = functional_mgr_badge

Sunil Nookala
Oracle DBA
Dell Corporation
Austin, TX

-Original Message-
Sent: Friday, August 31, 2001 3:20 PM
To: Multiple recipients of list ORACLE-L


Oracle has a very good section on prior and connect by.
These combined level let you walk a tree - so to speak.

I use them all the time. Look up the key words. You'll find it.

-Original Message-
Sent: Friday, August 31, 2001 12:07 PM
To: Multiple recipients of list ORACLE-L


Hi,

I am stumped on how to do a particular hierarchical
query. The query needs to be written entirely in SQL.

I have a table (ACCOUNT) with 3 columns of interest in
it, ACCOUNT_ID, ROOT_ID, PARENT_ID. Every row
represents an account. If an account is the root
(top-most), then the ROOT_ID and PARENT_ID columns are
null. If an account is a parent, then the ROOT_ID is
populated but the PARENT_ID is null. Accounts can be
nested multiple levels deep.

I need to find all of the accounts that belong to the
root of the given account but not include any accounts
that are children of the given account. Does this make
sense?

I.e.  5
  6   7
   8 9  10
11   1213  14

I'm not sure if my tree diagram will be dispalyed
properly after I email this but in essence if 8 is
the given account number, I want everything returned
except for 8, 11 and 12.

Thanks in advance!
-w




__
Do You Yahoo!?
Get email alerts  NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Walter K
  INET: [EMAIL PROTECTED]

Fat City Network Services-- (858) 538-5051  FAX: (858) 538-5051
San Diego, California-- Public Internet access / Mailing Lists

To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: John Lewis
  INET: [EMAIL PROTECTED]

Fat City Network Services-- (858) 538-5051  FAX: (858) 538-5051
San Diego, California-- Public Internet access / Mailing Lists

To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: 
  INET: [EMAIL PROTECTED]

Fat City Network Services-- (858) 538-5051  FAX: (858) 538-5051
San Diego, California-- Public Internet access / Mailing Lists

To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).



RE: Tricky Hierarchical Query

2001-08-31 Thread Jacques Kilchoer
Title: RE: Tricky Hierarchical Query





What would the data in your table look like, for the tree in the example?
e.g. 6 is a child of 5, but a parent of 8 and 9. Would the data look like this?


account_id root_id parent_id
 5 null null
 6 5 null
 8 5 6
 9 5 6



 -Original Message-
 From: Walter K [mailto:[EMAIL PROTECTED]]
 
 I am stumped on how to do a particular hierarchical
 query. The query needs to be written entirely in SQL.
 
 I have a table (ACCOUNT) with 3 columns of interest in
 it, ACCOUNT_ID, ROOT_ID, PARENT_ID. Every row
 represents an account. If an account is the root
 (top-most), then the ROOT_ID and PARENT_ID columns are
 null. If an account is a parent, then the ROOT_ID is
 populated but the PARENT_ID is null. Accounts can be
 nested multiple levels deep.
 
 I need to find all of the accounts that belong to the
 root of the given account but not include any accounts
 that are children of the given account. Does this make
 sense?
 
 I.e. 5
 6 7
 8 9 10
 11 12 13 14
 
 I'm not sure if my tree diagram will be dispalyed
 properly after I email this but in essence if 8 is
 the given account number, I want everything returned
 except for 8, 11 and 12.






RE: Tricky Hierarchical Query

2001-08-31 Thread Walter K

Sorry. I mis-stated the relationship to the parent in
my original email. Parent_id is always populated
unless the row is a root row.

The data for all rows in my sample tree would be:

ACCOUNT_ID   ROOT_ID   PARENT_ID
5null  null
65 5
75 5
85 6
95 6
10   5 7
11   5 8
12   5 8
13   5 9
14   5 10

-w

--- Jacques Kilchoer [EMAIL PROTECTED]
wrote:
 What would the data in your table look like, for the
 tree in the example?
 e.g. 6 is a child of 5, but a parent of 8 and 9.
 Would the data look
 like this?
 
 account_id root_idparent_id
5  null  null
6   5null
8   5 6
9   5 6
 
 
  -Original Message-
  From: Walter K [mailto:[EMAIL PROTECTED]]
  
  I am stumped on how to do a particular
 hierarchical
  query. The query needs to be written entirely in
 SQL.
  
  I have a table (ACCOUNT) with 3 columns of
 interest in
  it, ACCOUNT_ID, ROOT_ID, PARENT_ID. Every row
  represents an account. If an account is the root
  (top-most), then the ROOT_ID and PARENT_ID columns
 are
  null. If an account is a parent, then the
 ROOT_ID is
  populated but the PARENT_ID is null. Accounts can
 be
  nested multiple levels deep.
  
  I need to find all of the accounts that belong to
 the
  root of the given account but not include any
 accounts
  that are children of the given account. Does this
 make
  sense?
  
  I.e.  5
6   7
 8 9  10
  11   1213  14
  
  I'm not sure if my tree diagram will be dispalyed
  properly after I email this but in essence if 8
 is
  the given account number, I want everything
 returned
  except for 8, 11 and 12.
 
 
 


__
Do You Yahoo!?
Get email alerts  NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Walter K
  INET: [EMAIL PROTECTED]

Fat City Network Services-- (858) 538-5051  FAX: (858) 538-5051
San Diego, California-- Public Internet access / Mailing Lists

To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).



RE: Tricky Hierarchical Query

2001-08-31 Thread Jacques Kilchoer
Title: RE: Tricky Hierarchical Query





(P.S. - original requirement listed below)


The best I could come up with would be using connect by and prior, as suggested by another poster, in two queries with the minus set operator.

e.g.


SQL select * from account ;
ACCOUNT_ID ROOT_ID PARENT_ID
-- -- --
 5
 6 5 5
 7 5 5
 8 5 6
 9 5 6
 10 5 7
 11 5 8
 12 5 8
 13 5 9
 14 5 10
10 rows selected.


SQL -- first select gets the whole tree
SQL -- starting with the root for account 8
SQL -- second query gets 8 and its children
SQL select account_id
 2 from account
 3 connect by prior account_id = parent_id
 4 start with account_id = (select root_id from account where account_id = 8)
 5 minus
 6 select account_id
 7 from account
 8 connect by prior account_id = parent_id
 9 start with account_id = 8 ;


ACCOUNT_ID
--
 5
 6
 7
 9
 10
 13
 14


7 rows selected.


Jacques R. Kilchoër
x8816







 -Original Message-
 From: Walter K [mailto:[EMAIL PROTECTED]]
 Sent: vendredi, 31. août 2001 12:43
 To: Jacques Kilchoer; '[EMAIL PROTECTED]'
 Cc: '[EMAIL PROTECTED]'
 Subject: RE: Tricky Hierarchical Query
 
 
 Sorry. I mis-stated the relationship to the parent in
 my original email. Parent_id is always populated
 unless the row is a root row.
 
 The data for all rows in my sample tree would be:
 
 ACCOUNT_ID ROOT_ID PARENT_ID
 5 null null
 6 5 5
 7 5 5
 8 5 6
 9 5 6
 10 5 7
 11 5 8
 12 5 8
 13 5 9
 14 5 10
 
   -Original Message-
   From: Walter K [mailto:[EMAIL PROTECTED]]
   
   I am stumped on how to do a particular
  hierarchical
   query. The query needs to be written entirely in
  SQL.
   
   I have a table (ACCOUNT) with 3 columns of
  interest in
   it, ACCOUNT_ID, ROOT_ID, PARENT_ID. Every row
   represents an account. If an account is the root
   (top-most), then the ROOT_ID and PARENT_ID columns
  are
   null. If an account is a parent, then the
  ROOT_ID is
   populated but the PARENT_ID is null. Accounts can
  be
   nested multiple levels deep.
   
   I need to find all of the accounts that belong to
  the
   root of the given account but not include any
  accounts
   that are children of the given account. Does this
  make
   sense?
   
   I.e. 5
   6 7
   8 9 10
   11 12 13 14
   
   I'm not sure if my tree diagram will be dispalyed
   properly after I email this but in essence if 8
  is
   the given account number, I want everything
  returned
   except for 8, 11 and 12.