Parent-Child-Parent Sql
Hi:
I have two tables. My main table contains item and quantity. The second table contains material_item, item and quantity. They are joined on item. The next step is to check if the material_item contains in the item record in
the second table. If it does not, we insert the record into a brand new table. If it does, we will repeat the last step. We will check if the material_item is also an item record. It is parent/child relationship. But the
child can be a parent as well.
Table A
Item Quantity
123 1
234 2
Table B
Material Item Item Quantity
bbb 123 2
pop bbb 1
wer pop 1
234 aaa 1
Item 123 contains material item bbb, pop, wer (3 records)
Item 234 (1 record)
End Result - I will need to insert 4 records into the database.
I am trying to explain this not sure if I explain this clearly. Hopefully, the example will give you more insight.
I have been staring at this for quite a while. I am hoping a fresh pairs eyes will be able to tell me on what I need to do.
Thanks for your assistance.
kkmick
It seems to me that your example is the opposite of what is stated in the question "If it does not, we insert the record into a brand new table"
If this is what you want try something like
Insert into new table ....
where not exists (select item from materialitem)
Andy Tauber
Data Architect
The Vancouver Clinic
Website | LinkedIn
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click
"Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.
Similar Messages
-
Hi all,
I am trying to write an sql - and the requirement is as below -
Table data contains columns Parent || Child || Summary_Flag
If Child is Parent again to some other Child then Summary Flag is "Y"
e.g. parent || Child || Summary_flag
P1 C1 N
P2 P3 Y
P3 C2 N
This relation exists to four level - .i.e. level to which child can be parent again.
I am trying to write SQL which will give me data in columns - i.e. Parent1||Parent2||Parent3||Parent4||Child.
Pleas help in any possible way.
thanks
BhushanhI,
What is your desired output ?
Like this.
P1|C1
P2|P3|C2
SELECT REGEXP_REPLACE (MAX (res)KEEP (DENSE_RANK LAST ORDER BY lvl),
) res
FROM (SELECT PARENT,
SYS_CONNECT_BY_PATH ( CASE
WHEN LEVEL = 1
THEN PARENT
END
|| '|'
|| CHILD,
) res,
LEVEL lvl
FROM test_data
WHERE summary_flag = 'N'
CONNECT BY PRIOR CHILD = PARENT)
GROUP BY PARENT
SQL> WITH test_data AS
2 (SELECT 'P1' PARENT, 'C1' CHILD, 'N' summary_flag
3 FROM DUAL
4 UNION ALL
5 SELECT 'P2' PARENT, 'P3' CHILD, 'Y' summary_flag
6 FROM DUAL
7 UNION ALL
8 SELECT 'P3' PARENT, 'P4' CHILD, 'Y' summary_flag
9 FROM DUAL
10 UNION ALL
11 SELECT 'P4' PARENT, 'C2' CHILD, 'N' summary_flag
12 FROM DUAL)
13 SELECT REGEXP_REPLACE (MAX (res)KEEP (DENSE_RANK LAST ORDER BY lvl),
14 '/'
15 ) res
16 FROM (SELECT PARENT,
17 SYS_CONNECT_BY_PATH ( CASE
18 WHEN LEVEL = 1
19 THEN PARENT
20 END
21 || '|'
22 || CHILD,
23 '/'
24 ) res,
25 LEVEL lvl
26 FROM test_data
27 WHERE summary_flag = 'N'
28 CONNECT BY PRIOR CHILD = PARENT)
29 GROUP BY PARENT;
RES
P1|C1
P2|P3|P4|C2
SQL> Regards salim. -
Perfomance Issue on Parent Child table SQL
I am having table a and child table b contains data and message .
both are linked with id column. how can i pick the latest message from b against id . how can we make fastest sql.
I tried with subquries but it is taking time and working slow.
When i checking the cost of the sql in plan it is showing more than which i am expecting
Edited by: SA123 on Jun 22, 2009 3:22 PMOld Structure output
PLAN_TABLE_OUTPUT
| 0 | SELECT STATEMENT | | 194 | 149K|
591 (7)| 00:00:08 |
| 1 | SORT ORDER BY | | 194 | 149K|
591 (7)| 00:00:08 |
|* 2 | HASH JOIN | | 194 | 149K|
590 (7)| 00:00:08 |
|* 3 | HASH JOIN RIGHT OUTER | | 192 | 142K|
504 (8)| 00:00:07 |
PLAN_TABLE_OUTPUT
| 4 | VIEW | | 5 | 2535 |
288 (4)| 00:00:04 |
|* 5 | FILTER | | | |
| |
| 6 | HASH GROUP BY | | 5 | 570 |
288 (4)| 00:00:04 |
|* 7 | HASH JOIN | | 32116 | 3575K|
PLAN_TABLE_OUTPUT
283 (2)| 00:00:04 |
| 8 | TABLE ACCESS FULL | b | 31837 | 373K|
141 (2)| 00:00:02 |
| 9 | TABLE ACCESS FULL | b | 31837 | 3171K|
141 (2)| 00:00:02 |
| 10 | NESTED LOOPS | | 192 | 48768 |
216 (13)| 00:00:03 |
New Structure
PLAN_TABLE_OUTPUT
| 0 | SELECT STATEMENT | | 196 | 152K|
| 1108 (4)| 00:00:14 |
| 1 | SORT ORDER BY | | 196 | 152K|
| 1108 (4)| 00:00:14 |
|* 2 | HASH JOIN | | 196 | 152K|
| 1107 (4)| 00:00:14 |
|* 3 | HASH JOIN | | 193 | 145K|
| 1021 (4)| 00:00:13 |
PLAN_TABLE_OUTPUT
| 4 | NESTED LOOPS | | 192 | 48192 |
| 216 (13)| 00:00:03 |
|* 5 | HASH JOIN | | 3832 | 419K|
| 206 (11)| 00:00:03 | -
Hi,
In a table I got hierarchial data (parent child), where parent is child to some other parent.
This is upto level4.(I mean Child=>Parent1=>Parent2=>Grand Parent)
Child=>Parent
A1=>B1
A2=>B1
A3=>B1
B1=>B2
B2=>B3
B3=>B4
I need result like - Each child will have its all parents and subsequent parent
Like :
Child Parent1 Parent2 Grand Parent
=========================
A1=>B1 -- First Parent of A1
A1=>B2 -- Second Parent of A1
A1=>B3 -- Third Parent of A1
A1=>B4 -- Grand Parent
Please help. Is this possible with connect by feature? I tired using it but i am getting only parents for each.
Thanks
BhushanHi, Bhusan,
To repeat some value from the first row on all of the rows connected to it, use CONNECT_BY_ROOT
SELECT CONNECT_BY_ROOT child AS starting_point
, parent
FROM table_x
START WITH child = 'A1' -- or whatever
CONNECT BY chilD = PRIOR parent; -
Retrieve Parent Child Sub-Child recors in SQL Query
I've a self reference table and it has Parent-Child-Subchild relation. I'm getting an output (from second answer http://stackoverflow.com/questions/16031000/get-records-containing-all-the-child-records-in-sql-server) as expected, but it's
displaying results in below format.
<parent id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<sub child id> - <name> - <code>
<sub child id> - <name> - <code>
<sub child id> - <name> - <code>
I want to display the results like in below format with applying some indent style to distinguish parent\ child\sub-child relationship.
<parent id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<sub child id> - <name> - <code>
<sub child id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<sub child id> - <label> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
Please assist me on foumulating results as expected.BOL has a good example of doing this in the CTE topic. See http://technet.microsoft.com/en-us/library/ms175972%28v=sql.105%29.aspx Here is a slightly modified
version of what is in BOL.
CREATE TABLE #MyEmployees
EmployeeID smallint NOT NULL,
FirstName nvarchar(30) NOT NULL,
LastName nvarchar(40) NOT NULL,
Title nvarchar(50) NOT NULL,
DeptID smallint NOT NULL,
ManagerID int NULL,
CONSTRAINT PK_EmployeeID PRIMARY KEY CLUSTERED (EmployeeID ASC)
-- Populate the table with values.
INSERT INTO #MyEmployees VALUES
(1, N'Ken', N'Sánchez', N'Chief Executive Officer',16,NULL)
,(273, N'Brian', N'Welcker', N'Vice President of Sales',3,1)
,(274, N'Stephen', N'Jiang', N'North American Sales Manager',3,273)
,(275, N'Michael', N'Blythe', N'Sales Representative',3,274)
,(276, N'Linda', N'Mitchell', N'Sales Representative',3,274)
,(285, N'Syed', N'Abbas', N'Pacific Sales Manager',3,273)
,(286, N'Lynn', N'Tsoflias', N'Sales Representative',3,285)
,(16, N'David',N'Bradley', N'Marketing Manager', 4, 273)
,(23, N'Mary', N'Gibson', N'Marketing Specialist', 4, 16);
; WITH DirectReports(Name, Title, EmployeeID, EmployeeLevel, Sort)
AS (SELECT CONVERT(varchar(255), e.FirstName + ' ' + e.LastName),
e.Title,
e.EmployeeID,
1,
CONVERT(varchar(255), e.FirstName + ' ' + e.LastName)
FROM #MyEmployees AS e
WHERE e.ManagerID IS NULL
UNION ALL
SELECT CONVERT(varchar(255), REPLICATE (' ' , EmployeeLevel) +
e.FirstName + ' ' + e.LastName),
e.Title,
e.EmployeeID,
EmployeeLevel + 1,
CONVERT (varchar(255), RTRIM(Sort) + '| ' + FirstName + ' ' +
LastName)
FROM #MyEmployees AS e
JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID
SELECT EmployeeID, Name, Title, EmployeeLevel
FROM DirectReports
ORDER BY Sort;
GO
drop table #MyEmployees;
Tom -
SQL for Parent-Child Hierarchy
Please suggest which would be best way to achieve the below logic.
SQL to pick up parent child relationship within same table with a certain logic.
Example:
mod_product_number Product_Hierarchy
H555888 PH05678
H888987 H555888
H8889 H555888
H9955 H555888
H999999 H555888
P6666 H999999
P5555 H999999
Example: I expect the rows with H8889,H9955 & P6666 & P5555 to be sub-category values value for product hierarchy H555888.
If there are rows with H8888987 as Product_hierarchy, we will pull up those rows too for product hierarchy H555888.
The extra condition is we drill down only on 7 character mod_prod_number not on 5 character mod_prod_number. We pull out all sub category mod_prod_number for all distinct Product hierarchy.You can use Hierarchical Queries
See.. http://docs.oracle.com/cd/E11882_01/server.112/e10592/queries003.htm
select lpad(' ',2*(level-1)) || to_char(trim(t.mod_product_number)) prod,
SYS_CONNECT_BY_PATH(t.product_hierarchy, '/') "Path",
LEVEL
from temp_table t
start with trim(t.product_hierarchy) = 'PH05678'
connect by prior trim(t.mod_product_number) = trim(t.product_hierarchy);
. PROD Path LEVEL
1 H555888 /PH05678 1
2 H8889 /PH05678 /H555888 2
3 H888987 /PH05678 /H555888 2
4 H9955 /PH05678 /H555888 2
5 H999999 /PH05678 /H555888 2
6 P5555 /PH05678 /H555888 /H999999 3
7 P6666 /PH05678 /H555888 /H999999 3 -
Options for loading parent-child SQL 2005 tables
I was reading about a few examples of using Table types and a stored procedure to load tables that are joined by a foreign key (parent-child). Seems straight forward enough. However, that requires at least SQL 2008 and the system we are using
is on 2005 still for the time being. As much as I would like to upgrade, there is no time for it, even though we have other 2008 and 2012 instances. So with that said, what are my options for getting data from BTS to SQL where the destination is 2 tables
that have relations.
There are probably a dozen or more, but my first thought is a stored procedure with an xml parameter, then use xquery and xpath statements to pull the data out and insert. TRY/CATCH and RAISEERROR to return error codes back to the BTS application.
Thoughts?First, how many records are you dealing with? I don't even think of terms of 'bulk' until 10,000 or so. And once that threshold is crossed, the first tool out of the box is SSIS.
Second, are you debatching or is there a triple confirmed requirement that all records be loaded in a single transaction?
Finally, the 'DB round-trip' issue is often misunderstood in real world settings. In a typical db transaction, the most expensive operation is establishing the connection. Once that's done, the SQL Client is very efficient in transporting data
to and from the database. Since the wcfSqlBinding uses a Connection Pool, that setup is only done once to some high-water mark.
Consider, the data has to be sent no matter what and in every conseivable scneario*, the byte count of a stored procedure over TDS would be less than sending the same data wrapped in Xml. The network adds considerable latency to the conversation.
This is why I'm skeptical of using OPENXML() without a provable benefit.
So, don't get caught up in theoritical 'performance' issues without understanding the entire scenario first.
*SQL Code being the primary exception. -
Help needed on Many to Many Parent Child Relationship (PL/SQL)
Friends,
Please help me.
I have table where in two columns are having many to many parent child relation ship. It means child can have multiple parents.
Table strucuture and sample records given below. Here 334059 is GRAND PARENT of all child records.
PARENT CHILD LEVEL Usage
334059 380499 1 This Level has 2 childs
334059 334730 1
334730 335629 2 This level too has 2 childs where in 380499 is child of 334730 & 334059 also.
334730 380499 2
380499 380497 2 This level has 3 childs and 88888 is having further child.
380499 334730 2
380499 88888 2
88888 99999 3 It has one child 99999 and has further child 10101.
99999 10101 4 It has one child 10101 and has further child 11111.
10101 11111 5 It has one child 11111 and has no further child. I.e leaf node.
11111 0 6 leaf node
What I want is records in same level above. I am using below code but its implementing only UPTO 2nd LEVEL given above.
Further its not going i.e 3,4,5,6 level. Could you please help me?
DECLARE
CURSOR main_pc(p_child pc.child%type) IS
SELECT CHILD
FROM pc where parent = p_child;
CURSOR main_dw(p_child dw_exrdbo.exr_parent_child.child_fund%type,
p_level NUMBER) IS
SELECT DISTINCT CHILD_FUND,LEVEL_NUMBER FROM dw_exrdbo.exr_parent_child
where parent_fund = p_child
and parent_fund <> child_fund
and level_number = p_level;
v_next_parent pc.child%type;
v_level NUMBER := 1;
v_grand_parent dw_exrdbo.exr_parent_child.parent_fund%type := 334059;
v_parent dw_exrdbo.exr_parent_child.parent_fund%type;
vDone BOOLEAN;
v_xyz dw_exrdbo.exr_parent_child.parent_fund%type;
v_cnt NUMBER := 0;
BEGIN
DELETE FROM dw_exrdbo.exr_parent_child;
COMMIT;
INSERT INTO dw_exrdbo.exr_parent_child
SELECT PARENT, CHILD, 'INVONE', 12345, 'P',0,1
FROM pc where parent = v_grand_parent
AND NOT EXISTS (select 'x' from dw_exrdbo.exr_parent_child where parent_fund = v_grand_parent);
COMMIT;
FOR i IN (select distinct child FROM pc WHERE parent = v_grand_parent) --334059
LOOP
DBMS_OUTPUT.PUT_LINE('Next Parent is '||v_parent);
FOR x IN main_pc(i.child)
LOOP
INSERT INTO dw_exrdbo.exr_parent_child
(parent_fund, child_fund, source_application, valuation_date_sid, parent_child_flag, child_count, level_number)
VALUES(i.child, X.CHILD, 'INVONE', 12345, 'C',0,1+1);
COMMIT;
END LOOP;
END LOOP;
END;
Thanks in advance!!!
Regards,
JiggerHi, Jigger,
Sorry, I can't tell what you want.
Whenever you have a question, please post CREATE TABLE and INSERT statements for your sample data, and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using. -
Getting parent-child data from a single column in a table
Hi,
I have a parent-child data in a column.
Eg:
0
00
01
010
011
1
10
11
These values are present in the single column itself. My need is to get the parent values for the given child value.
For eg: If I am giving the input as 011, the query should return all its parents, i.e. 01 and 0
Could you please give me any inputs on this?
Thanks,
GVFrank Kulash wrote:
Assuming that each child's id is formed by adding exactly one character to the end of its parent's id:
SELECT id
FROM table_x
START WITH id = :target_id
CONNECT BY PRIOR id LIKE id || '_';
Small tweak to yours Frank if it's just the parents that need identifying...
SQL> ed
Wrote file afiedt.buf
1 with t as (select '0' as x from dual union all
2 select '00' from dual union all
3 select '01' from dual union all
4 select '010' from dual union all
5 select '011' from dual union all
6 select '1' from dual union all
7 select '10' from dual union all
8 select '11' from dual)
9 -- end of test data
10 SELECT x
11 FROM t, (select '&required' as req from dual) req
12 WHERE x != req.req
13 START WITH x = req.req
14* CONNECT BY PRIOR x LIKE x || '_'
SQL> /
Enter value for required: 011
old 11: FROM t, (select '&required' as req from dual) req
new 11: FROM t, (select '011' as req from dual) req
X
01
0
SQL> /
Enter value for required: 11
old 11: FROM t, (select '&required' as req from dual) req
new 11: FROM t, (select '11' as req from dual) req
X
1
SQL> -
Insert Record with Parent/Child Tables doesn't work with Oracle - unlike AC
Hi,
I just Migrated a MS Access 2010 Database to an Oracle 11g Backend with the SQL Developer Tool.
The Migration went fine, all the Tables and Views are migrated.
I'm working with MS Access as Frontend.
The application has some Datasheets with Subdatasheets with Parent/Child Relationship. (1-n Relationship)
After changing to Oracle, it's not possible, to Insert a new Record in a Subdatasheet I always get the following Error Message:
"The Microsoft Access database engine cannot find a record in the table 'xxxx' with key matching field(s) 'zzzzz'"
It used to work perfect with the MS Access Backend, do I need a trigger which first adds the child Record ?
Or what should I do?
Thank youHi Klaus,
Thanks for your answer. I still haven't solved my problem. The only way would be to use a singel 1:n Relationship, but in fact I need a n:m Relationship.
I tried the same scenario with a new Access Application, same result.
To clearify my problem.
Goal: Parent Form with Parent Records, Linked Child Form with Child Records in a Datasheet View => Insert of a NEW Child Record.
I have 3 Tables (table1 = Parent tabel, table2 = Child Table, table12 = n:m Tabel with PK and two FK)
The Recordsource of the Parent Form is Tabel1
The Recordsource of the Child Form is Table2 joined with Table12.
In my Old Access Project, Access Triggered the Insert and filled Table12 with the NEW PK of Table2.
It seems like Access can't do that anymore....
I'm pretty desperate and I'm sure it is just a litte thing to fix..... -
How to delete parent child relation in Toplink
Hi All,
I have 3 tables A,B,C.
In Table A ,I am saving record.
Table B & C has parent child relation.
B-->Parent
C-->Child
So I want to save records in Table A.
And delete from child(C) 1st then from parent(B).
I m writing my code as,
em.getTransaction().begin();
em.persist(Table A);//save in Table A
em.remove(em.merge(Table B));//Remove from Parent
But how to delete records from child table then from parent table.
Thanks
SandipIf you have a @OneToOne relationship between two entities, the join column information is used to order the SQL when you remove two entities. For example, if I have:
@Entity
public class Employee implements Serializable {
@OneToOne
@JoinColumn(name="ADDR_ID")
private Address address;
...Then the following code runs regardless of the order of the remove calls.
em.getTransaction().begin();
Employee parent = new Employee();
Address child = new Address();
parent.setAddress(child);
em.persist(parent);
em.persist(child);
em.getTransaction().commit();
em.getTransaction().begin();
parent = em.merge(parent);
child = em.merge(child);
// order of next two statements unimportant
em.remove(parent);
em.remove(child);
em.getTransaction().commit();If I don't remove the parent and just the child I get the same error you do because of the FK from Employee to Address.
--Shaun -
Dynamic hierarchy in parent child hierarchy table??
Do you have any experience to handle the requirement of dynamic hierarchy in universe/webi?
We have some data in parent child hierarchy as below u201CCustomeru201D table.
Customer
Parent Child
Z A
Z B
A AA
B BB
AA AAA
For example, Company Z is the parent company of Company A.
Another table, Amount is the amount value of different Customers.
Amount
ID Amt
AA 10
AAA 1
BB 2
Is there any functionality in Universe designer to build related Classes and objects, So that the web intelligence documents represent the following report with the drilling results?
When we want to see the ID and Amt, the expected result should be:
Z 13
when we drill down Z
the result should be:
A 11
B 2
when we drill down A:
the result should be:
AA 11
notes **
the level of hierarchy is dynamic
Any suggestion is appreciated. Thanks.Hi,
The only way to do it is to create recursive derived table that flatten you parent child hierarchy with a given maximum depth.
Here is a sample I built a long time ago to flatten a parent-child hierarchy on Employees table in Foodmart database (SQL Server).
Didier
SELECT DISTINCT
Z.employee_id,
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
A.supervisor_id_5,
A.employee_id_5,
A.full_name_5,
A.supervisor_id_6,
A.employee_id_6,
A.full_name_6,
Z.supervisor_id AS supervisor_id_7,
Z.employee_id AS employee_id_7,
Z.full_name AS full_name_7
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
A.supervisor_id_5,
A.employee_id_5,
A.full_name_5,
Z.supervisor_id AS supervisor_id_6,
Z.employee_id AS employee_id_6,
Z.full_name AS full_name_6
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
Z.supervisor_id AS supervisor_id_5,
Z.employee_id AS employee_id_5,
Z.full_name AS full_name_5
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
Z.supervisor_id AS supervisor_id_4,
Z.employee_id AS employee_id_4,
Z.full_name AS full_name_4
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
Z.supervisor_id AS supervisor_id_3,
Z.employee_id AS employee_id_3,
Z.full_name AS full_name_3
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
Z.supervisor_id AS supervisor_id_2,
Z.employee_id AS employee_id_2,
Z.full_name AS full_name_2
FROM employee Z,
SELECT DISTINCT
supervisor_id AS supervisor_id_1,
employee_id AS employee_id_1,
full_name AS full_name_1
FROM employee
WHERE supervisor_id = 0 OR supervisor_id IS NULL
) A
WHERE A.employee_id_1 = Z.supervisor_id
) A
WHERE A.employee_id_2 = Z.supervisor_id
) A
WHERE A.employee_id_3 = Z.supervisor_id
) A
WHERE A.employee_id_4 = Z.supervisor_id
) A
WHERE A.employee_id_5 = Z.supervisor_id
) A
WHERE A.employee_id_6 = Z.supervisor_id
UNION
SELECT DISTINCT
A.employee_id_6 AS employee_id,
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
A.supervisor_id_5,
A.employee_id_5,
A.full_name_5,
A.supervisor_id_6,
A.employee_id_6,
A.full_name_6,
NULL AS supervisor_id_7,
NULL AS employee_id_7,
NULL AS full_name_7
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
A.supervisor_id_5,
A.employee_id_5,
A.full_name_5,
Z.supervisor_id AS supervisor_id_6,
Z.employee_id AS employee_id_6,
Z.full_name AS full_name_6
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
Z.supervisor_id AS supervisor_id_5,
Z.employee_id AS employee_id_5,
Z.full_name AS full_name_5
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
Z.supervisor_id AS supervisor_id_4,
Z.employee_id AS employee_id_4,
Z.full_name AS full_name_4
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
Z.supervisor_id AS supervisor_id_3,
Z.employee_id AS employee_id_3,
Z.full_name AS full_name_3
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
Z.supervisor_id AS supervisor_id_2,
Z.employee_id AS employee_id_2,
Z.full_name AS full_name_2
FROM employee Z,
SELECT DISTINCT
supervisor_id AS supervisor_id_1,
employee_id AS employee_id_1,
full_name AS full_name_1
FROM employee
WHERE supervisor_id = 0 OR supervisor_id IS NULL
) A
WHERE A.employee_id_1 = Z.supervisor_id
) A
WHERE A.employee_id_2 = Z.supervisor_id
) A
WHERE A.employee_id_3 = Z.supervisor_id
) A
WHERE A.employee_id_4 = Z.supervisor_id
) A
WHERE A.employee_id_5 = Z.supervisor_id
) A
UNION
SELECT DISTINCT
A.employee_id_5 AS employee_id,
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
A.supervisor_id_5,
A.employee_id_5,
A.full_name_5,
NULL AS supervisor_id_6,
NULL AS employee_id_6,
NULL AS full_name_6,
NULL AS supervisor_id_7,
NULL AS employee_id_7,
NULL AS full_name_7
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
Z.supervisor_id AS supervisor_id_5,
Z.employee_id AS employee_id_5,
Z.full_name AS full_name_5
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
Z.supervisor_id AS supervisor_id_4,
Z.employee_id AS employee_id_4,
Z.full_name AS full_name_4
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
Z.supervisor_id AS supervisor_id_3,
Z.employee_id AS employee_id_3,
Z.full_name AS full_name_3
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
Z.supervisor_id AS supervisor_id_2,
Z.employee_id AS employee_id_2,
Z.full_name AS full_name_2
FROM employee Z,
SELECT DISTINCT
supervisor_id AS supervisor_id_1,
employee_id AS employee_id_1,
full_name AS full_name_1
FROM employee
WHERE supervisor_id = 0 OR supervisor_id IS NULL
) A
WHERE A.employee_id_1 = Z.supervisor_id
) A
WHERE A.employee_id_2 = Z.supervisor_id
) A
WHERE A.employee_id_3 = Z.supervisor_id
) A
WHERE A.employee_id_4 = Z.supervisor_id
) A
UNION
SELECT DISTINCT
A.employee_id_4 AS employee_id,
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
A.supervisor_id_4,
A.employee_id_4,
A.full_name_4,
NULL AS supervisor_id_5,
NULL AS employee_id_5,
NULL AS full_name_5,
NULL AS supervisor_id_6,
NULL AS employee_id_6,
NULL AS full_name_6,
NULL AS supervisor_id_7,
NULL AS employee_id_7,
NULL AS full_name_7
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
Z.supervisor_id AS supervisor_id_4,
Z.employee_id AS employee_id_4,
Z.full_name AS full_name_4
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
Z.supervisor_id AS supervisor_id_3,
Z.employee_id AS employee_id_3,
Z.full_name AS full_name_3
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
Z.supervisor_id AS supervisor_id_2,
Z.employee_id AS employee_id_2,
Z.full_name AS full_name_2
FROM employee Z,
SELECT DISTINCT
supervisor_id AS supervisor_id_1,
employee_id AS employee_id_1,
full_name AS full_name_1
FROM employee
WHERE supervisor_id = 0 OR supervisor_id IS NULL
) A
WHERE A.employee_id_1 = Z.supervisor_id
) A
WHERE A.employee_id_2 = Z.supervisor_id
) A
WHERE A.employee_id_3 = Z.supervisor_id
) A
UNION
SELECT DISTINCT
A.employee_id_3 AS employee_id,
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
A.supervisor_id_3,
A.employee_id_3,
A.full_name_3,
NULL AS supervisor_id_4,
NULL AS employee_id_4,
NULL AS full_name_4,
NULL AS supervisor_id_5,
NULL AS employee_id_5,
NULL AS full_name_5,
NULL AS supervisor_id_6,
NULL AS employee_id_6,
NULL AS full_name_6,
NULL AS supervisor_id_7,
NULL AS employee_id_7,
NULL AS full_name_7
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
Z.supervisor_id AS supervisor_id_3,
Z.employee_id AS employee_id_3,
Z.full_name AS full_name_3
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
Z.supervisor_id AS supervisor_id_2,
Z.employee_id AS employee_id_2,
Z.full_name AS full_name_2
FROM employee Z,
SELECT DISTINCT
supervisor_id AS supervisor_id_1,
employee_id AS employee_id_1,
full_name AS full_name_1
FROM employee
WHERE supervisor_id = 0 OR supervisor_id IS NULL
) A
WHERE A.employee_id_1 = Z.supervisor_id
) A
WHERE A.employee_id_2 = Z.supervisor_id
) A
UNION
SELECT DISTINCT
A.employee_id_2 AS employee_id,
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
A.supervisor_id_2,
A.employee_id_2,
A.full_name_2,
NULL AS supervisor_id_3,
NULL AS employee_id_3,
NULL AS full_name_3,
NULL AS supervisor_id_4,
NULL AS employee_id_4,
NULL AS full_name_4,
NULL AS supervisor_id_5,
NULL AS employee_id_5,
NULL AS full_name_5,
NULL AS supervisor_id_6,
NULL AS employee_id_6,
NULL AS full_name_6,
NULL AS supervisor_id_7,
NULL AS employee_id_7,
NULL AS full_name_7
FROM employee Z,
SELECT DISTINCT
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
Z.supervisor_id AS supervisor_id_2,
Z.employee_id AS employee_id_2,
Z.full_name AS full_name_2
FROM employee Z,
SELECT DISTINCT
supervisor_id AS supervisor_id_1,
employee_id AS employee_id_1,
full_name AS full_name_1
FROM employee
WHERE supervisor_id = 0 OR supervisor_id IS NULL
) A
WHERE A.employee_id_1 = Z.supervisor_id
) A
UNION
SELECT DISTINCT
A.employee_id_1 AS employee_id,
A.supervisor_id_1,
A.employee_id_1,
A.full_name_1,
NULL AS supervisor_id_2,
NULL AS employee_id_2,
NULL AS full_name_2,
NULL AS supervisor_id_3,
NULL AS employee_id_3,
NULL AS full_name_3,
NULL AS supervisor_id_4,
NULL AS employee_id_4,
NULL AS full_name_4,
NULL AS supervisor_id_5,
NULL AS employee_id_5,
NULL AS full_name_5,
NULL AS supervisor_id_6,
NULL AS employee_id_6,
NULL AS full_name_6,
NULL AS supervisor_id_7,
NULL AS employee_id_7,
NULL AS full_name_7
FROM employee Z,
SELECT DISTINCT
supervisor_id AS supervisor_id_1,
employee_id AS employee_id_1,
full_name AS full_name_1
FROM employee
WHERE supervisor_id = 0 OR supervisor_id IS NULL
) A -
Parent - child tables foreign key relation ship
Hi All,
we have 600 hundred tables ..i would like to print a hierarchial tree based on the foreigh key relathionship
i tried my best but i couldnt.
parent table
child table 1
childtable3
child table 2
and so
Can somebody help me out with a query?
Thanks,
ktCREATE OR REPLACE FUNCTION get_child_tables (
ptable VARCHAR2,
powner VARCHAR2 DEFAULT 'SCOTT',
plevel NUMBER DEFAULT 10
RETURN stringarray
-- -- create this ON SQL*PLUS "CREATE OR REPLACE TYPE STRINGARRAY AS TABLE OF VARCHAR2(50);"
-- AUTHID CURRENT_USER
PIPELINED
AUTHOR DATE VERSION COMMENTS
======================================================================================
[email protected] 26-OCT-2009 1.0 Developed to ease developers effort to find Nth level of Referential integrity
======================================================================================
-- PURPOSE -> To find PARENT=> CHILD relational TABLE(S) in Oracle upto a depth max N Level.
--SYNTAX TO USE
SELECT * FROM TABLE( get_child_tables('DEPT','SCOTT',3)); Store this query in a file for your use
SELECT * FROM TABLE( get_child_tables('EMPLOYEE')); Store this query in a file for your use
-- RESULTS looks as below
--1 => DEPT
--2 => EMP
--2 => EMP2
--3 => EMP_CHILD
--3 => EMP2_CHILD
-- and so on
--This can be leveraged to use in any oracle database REGION 10g having and above.
--This FUNCTION gives formatted result of the Oracle 10g Hierarchical query result coded in the cursor
--to find MASTER => CHILD relational TABLE(S) upto a depth max 10 Level.
--The result of the PIPELINED function can be retrieved using Oracle new operator
--TABLE(array name) in SQL query.
--Due to the AUTHID CURRENT_USER compiler directive any user can use based on his/her access privileges on the database.
--GRANT EXECUTE ON SCOTT.get_child_tables TO PUBLIC;
--CREATE OR REPLACE PUBLIC SYNONYM get_child_tables FOR SCOTT.get_child_tables;
IS
atname stringarray := stringarray ();
-- create this ON SQL*PLUS CREATE OR REPLACE TYPE STRINGARRAY AS TABLE OF VARCHAR2(50);
vlevel NUMBER;
vtname VARCHAR2 (50);
nindex NUMBER := 0;
bprocessed BOOLEAN := FALSE;
CURSOR c1 (powner_in IN VARCHAR2, ptable_in VARCHAR2, plevel_in NUMBER)
IS
SELECT LEVEL, LPAD (' ', (LEVEL - 1) * 2, ' ') || pt AS "TNAME"
FROM (SELECT a.owner w1, a.table_name pt, a.constraint_name c1,
a.r_constraint_name r1, b.owner w2, b.table_name ct,
b.constraint_name c2, b.r_constraint_name r2
FROM all_constraints a, all_constraints b
WHERE a.constraint_name = b.r_constraint_name(+)
AND a.owner = b.owner(+)
AND a.owner =
UPPER (powner)
-- Change Owner here while testing
--AND A.r_constraint_name IS NULL
AND a.constraint_type IN ('P', 'R')) v1
START WITH pt =
UPPER
(ptable)
-- Change your master table here while testing the QUERY
CONNECT BY PRIOR ct = pt AND LEVEL <= plevel;
-- Change lavel here while testing
BEGIN
atname.EXTEND;
atname (1) := 'NOTHING';
OPEN c1 (powner, ptable, plevel);
LOOP
bprocessed := FALSE;
FETCH c1
INTO vlevel, vtname;
IF nindex > 1 AND atname (atname.LAST - 1) = vtname
THEN
--DBMS_OUTPUT.PUT_LINE('2 ==== vtname ' ||vtname || ' '|| atname.count|| ' '||atname.last || ' '||atname( atname.last-1));
bprocessed := TRUE;
END IF;
IF NOT bprocessed
THEN
nindex := nindex + 1;
atname.EXTEND;
atname (nindex) := vtname;
PIPE ROW (vlevel || ' => ' || vtname);
DBMS_OUTPUT.put_line ( ' **** nindex - atname( nindex) '
|| nindex
|| ' - '
|| atname (nindex)
DLOG('ADDING ',vTname); A LOGGING ATONOMUS PROCEDURE FOR DEBUG PURPOSE
END IF;
EXIT WHEN c1%NOTFOUND;
END LOOP;
CLOSE c1;
FOR i IN 1 .. atname.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE('atname (i) ' ||atname (i));
END LOOP;
RETURN;
EXCEPTION
WHEN no_data_needed
THEN -- THIS EXCEPTION HAS TO BE THERE TO GET THE FUCTION WORKABLE
DBMS_OUTPUT.put_line (SQLERRM);
RETURN;
END get_child_tables;
/ -
Error with Parent-Child Hierarchies in BIEE11.1.1.5
Hi ,
I am using BIEE11.1.1.5, I have created a Logical Dimensions with Parent-Child Hierarchies,
In answer,I draged the Hierarchies named 'H5 Sales Rep', then view the results,
It has error,error message as follows:
Odbc driver returned an error (SQLExecDirectW).
Error Details
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 43119] Query Failed: [nQSError: 22056] To use hierarchical functions, you need to associate a Closure Table with Logical Table Source 'LTS1 Sales Rep'. (HY000)
SQL Issued: SELECT 0 s_0, "Sample Sales"."Sales Rep"."Sales Rep Name" s_1, CASE WHEN ISLEAF("Sample Sales"."Sales Rep"."H5 Sales Rep") THEN 1 ELSE 0 END s_2, IDOF("Sample Sales"."Sales Rep"."H5 Sales Rep") s_3, PARENT("Sample Sales"."Sales Rep"."H5 Sales Rep") s_4 FROM "Sample Sales" WHERE ISROOT("Sample Sales"."Sales Rep"."H5 Sales Rep")
anyone know why???
Thank you!Hi Leo,
Did you configure closure table? http://www.rittmanmead.com/2010/11/oracle-bi-ee-11g-parent-child-hierarchies-multiple-modeling-methods/
Also make sure to perform the hierarchy changes in offline mode.
Regards,
Dpka -
Hi All
Fairly new at this and am a bit confused.
I have been given the task of cleaning up certain tables in my DB. I am running into parent / child issues.
I want to remove all entries from Table A. When i try and delete one row it gives me a constraint violation. It tells me there is a child record out there. So i go ahead and disable that constraint, go back to table A and try the delete again. I get a different another violation telling me that there is a child record on a different table. And on and on the story goes.
Now I could keep doing this but the schema i am working on has 1051 tables and just over 6000 constraints. So as you can see this might take me a month to sit here and do that. Not very keen on that.
What I am after is:
1. A program / script that will lay out all the parent child relationships for me. Coming across each one as i try and delete is painful
Would it be possible to get something like:
Table A
-- Table B
-- Table C
Table D
-- Table E
-- Table G
-- Table F
2. All the constraints have been created with a delete rule of 'No Action'. I think the easiest thing to do would be to go to the parent table (or what I think is the parent table) and change this to on delete Cascade. From what it looks like I don't think you can do this. Does anybody know how to? Or if its possible?
3. The third option I am thinking of is:
Disable All the constraints on what I think is the parent table. Remove entries from the child tables. Enable the constraints.
Now my issue here is, how do i know all of the child tables? I guess that goes back to my question 1.
Thanks in advance. Sorry for the story.
The lack of documentation / diagrams here is driving me nuts :)You can use tools like Microsoft Visio to reverse engineer the tables and their relationships.
You can also generate a list of parent-child related tables from user_constraints.
something like:
SQL> SELECT TABLE_NAME,
2 (SELECT TABLE_NAME
3 FROM ALL_CONSTRAINTS R
4 WHERE R.OWNER = U.R_OWNER
5 AND R.CONSTRAINT_NAME = U.R_CONSTRAINT_NAME) PARENT_TABLE
6 FROM USER_CONSTRAINTS U
7 WHERE U.CONSTRAINT_TYPE = 'R'
8 /
TABLE_NAME PARENT_TABLE
EMP DEPT
SQL>
Maybe you are looking for
-
Erasing hard drive problems on macbook pro
My brand new macbook pro was working very slowly for about a day and then just froze so I restarted it. But then when it was trying to turn on it was just a gray screen with a file and a question mark on it, which I read means it can't find the hard
-
Hello my laptop 42335MG and use the system Windows 7 Currently suffer from the problem of not being able to open camera or can't find application look this image http://6eer.co.tv/images/kvk.jpg There are two other were defined I do not have the know
-
My Categories and sub-categories are now missing
I installed new PSE12 on my Win7x64. I previously had PSE9 installed. My Categories and sub-categories are now missing? I need to know how to reinstall them and auto attach to my pictures? <moved by mod from downloading,installing,setting up - kglad>
-
Changed value in Appent not written to change document
Hello, I have added some fields to a table using an append. During test I realized that the function that creates the change document does not consider appended fields (I debugged deep into some SAP function to see why). This is a problem now. All ch
-
VAIO S13: battery not charging over 80%
Hi to all, I have a big question for you: Sony provides an utility (that of course runs only on win) that lets you set the maximum charge of the battery to 80% or even 50% (in order to avoid battery decay, they say). This program probably activates s