Home » SQL & PL/SQL » SQL & PL/SQL » finding missing parent from oracle hierarchical query (11g)
finding missing parent from oracle hierarchical query [message #611587] Fri, 04 April 2014 01:33 Go to next message
ashwanth77
Messages: 95
Registered: April 2013
Location: India
Member

i have a table with parent/child relationship. how do i find the missing parent

hierachiacl table

child parent
2000698835 2001455376

if the parent is not present in the customer table i need to insert it to customer table.
here how do i find the missing parent in customer table.

select * from customer where true_gcdb_source_key='2001455376'(which is the parent)
what i tried so far

select level, PARENT_GCDB_ID
from EU_SAP_CUST_REL_INIT
where level <=4
start with PARENT_GCDB_ID not in (
select true_gcdb_source_key
from customer
where true_gcdb_source_key=PARENT_GCDB_ID
)
connect by prior PARENT_GCDB_ID=CHILD_GCDB_ID
Re: finding missing parent from oracle hierarchical query [message #611588 is a reply to message #611587] Fri, 04 April 2014 01:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
i have a table with parent/child relationship. how do i find the missing parent


This should not be possible as there should be a foreign key. Razz

There is no need of a hierarchical query here, just use
select parent from customer where parent not in (select child from customer);

Re: finding missing parent from oracle hierarchical query [message #611590 is a reply to message #611588] Fri, 04 April 2014 02:00 Go to previous messageGo to next message
ashwanth77
Messages: 95
Registered: April 2013
Location: India
Member
i should insert all its parents,grandparents,greatgrandparents into customer table only if it is not present there
Re: finding missing parent from oracle hierarchical query [message #611592 is a reply to message #611590] Fri, 04 April 2014 02:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If parent is not known how can you know/insert a grand-parent?

Re: finding missing parent from oracle hierarchical query [message #611593 is a reply to message #611592] Fri, 04 April 2014 02:23 Go to previous messageGo to next message
ashwanth77
Messages: 95
Registered: April 2013
Location: India
Member
parent can be found in eg.select * from customer where true_gcdb_source_key='2001455376'

then in parent child tree
child parent
2000698835 2001455376
Re: finding missing parent from oracle hierarchical query [message #611595 is a reply to message #611593] Fri, 04 April 2014 02:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What you post is completely meaningless.
Your "select *" does not return the column you are conditioning. How is this possible?

Post a complete test case: create table and insert statements.

[Updated on: Fri, 04 April 2014 02:40]

Report message to a moderator

Re: finding missing parent from oracle hierarchical query [message #611614 is a reply to message #611595] Fri, 04 April 2014 08:01 Go to previous messageGo to next message
ashwanth77
Messages: 95
Registered: April 2013
Location: India
Member
select * from sap_cust_rel_init
start with child_gcdb_id='2002615591'
connect by child_gcdb_id=prior parent_gcdb_id


child_gcdb_id parent_gcdb_id
2002615591 2002554170
2002554170 2002554286
2002554286 2002554081
2002554081
2002554081
2002554081
2002554286 2002554081
2002554081
2002554081
2002554081
2002554286 2002554081
2002554081
2002554081
2002554081
2002554170 2002554286
2002554286 2002554081
2002554081
2002554081
2002554081
2002554286 2002554081
2002554081
2002554081
2002554081
2002554286 2002554081
2002554081
2002554081
2002554081
2002554170 2002554286
2002554286 2002554081
2002554081
2002554081
2002554081
2002554286 2002554081
2002554081
2002554081
2002554081
2002554286 2002554081

this is used to get all parents for that child

if any parents are not present in customer table,i need to insert them
Re: finding missing parent from oracle hierarchical query [message #611616 is a reply to message #611614] Fri, 04 April 2014 08:10 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>if the parent is not present in the customer table i need to insert it to customer table.
If PARENT does NOT exist, how do you determine which value is correct for the INSERT?
Re: finding missing parent from oracle hierarchical query [message #611617 is a reply to message #611614] Fri, 04 April 2014 08:25 Go to previous message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
So write INSERT statement:
insert into customer( <column list of customer table> )
with your_query as ( <the query you posted> )
   , parents as ( select distinct <column with parent> parent
                  from your_query
                  where <column with parent> is not null )
select <value expression list (one of the expressions should be parent)>
from parents;

Substitute text inside <> with your actual values (you did not post anything except the query).
Previous Topic: ORA-12899: value too large for column
Next Topic: incorrect output from my sql command
Goto Forum:
  


Current Time: Fri Apr 26 00:42:11 CDT 2024