PL/SQL :: Query To Fetch Parent Records Only If No Child With Particular Status?

Sep 16, 2012

I need to fech parent records only when no child record with status 'N' exists. There are only two possible values for status column of child table 'Y' / 'N'.

Below are table structures and insert statements for data.

CREATE TABLE MASTER
(
COL1  NUMBER,

[Code]....

COMMIT;Query I framed is below

select * from master where exists (select null from child where child.col2 = master.col1
group by child.col2 having count(distinct col3) =1 )

Output in above case would be 3 as for 1 there's one record with status as 'N' and for 2 there's no child record. I am on 10g.

View 2 Replies


ADVERTISEMENT

SQL & PL/SQL :: Delete Records From Child And Parent Table

Jan 2, 2013

I want to delete records from parent table which are less than 2 years. Before deleting records from parent table we have to delete records from child table. How can we delete those records. I don't want to use ON DELETE CASCADE.

MASS_MASTER --parent table.
MASS_CHILD --child table.

The below query is used to delete records from parent table.

DELETE FROM mass_master WHERE last_date<=ADD_MONTHS(sysdate,-24);

The child table MASS_CHILD is not having last_date column. provide me the query to delete same records from child table.

View 21 Replies View Related

SQL & PL/SQL :: Fetch Records Based On 2 Status

Jan 25, 2013

create table test_exp (oid number, ioid number, status varchar2(20));

Below are the insert statements.

Insert into TEST_EXP (OID, IOID, STATUS) Values (1, 100, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (1, 101, 'DISCO');
Insert into TEST_EXP (OID, IOID, STATUS) Values (1, 102, 'CANCELLED');
Insert into TEST_EXP (OID, IOID, STATUS) Values (2, 103, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (3, 104, 'DISCO');
Insert into TEST_EXP (OID, IOID, STATUS) Values (4, 105, 'DISCO');
Insert into TEST_EXP (OID, IOID, STATUS) Values (4, 106, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (5, 107, 'NEW');
Insert into TEST_EXP (OID, IOID, STATUS) Values (5, 108, 'CANCELLED');
[code]...

Now my problem is we should fetch the data based on the below rules

If an OID contains 2 IOIDs for which there is a NEW and DISCO status attached, then fetch the 2 records
If an OID has only 1 of these status, then ignore the same
If an OID has none of the 2 status, then ignore the same.

The expected output will be like below:

OID|IOID|STATUS
1 |100 |NEW
1 |101 |DISCO
4 |105 |DISCO
4 |106 |NEW

View 4 Replies View Related

SQL & PL/SQL :: Fetch Records Based On Status Priority

Dec 20, 2012

I have a table with below syntax and insert statements.

-- Table creation
-----------------------
create table order_test (oid number,
orderno number,
ordername varchar2(100),
orderstatus varchar2(50),
orderseq number);
----------------------------------
-- Insert statements

[Code]....

The Output looks like below

OID|ORDERNO|ORDERNAME|ORDERSTATUS|ORDERSEQ
1 |100 |ORD1 |INPROGRESS |1
7 |100 |ORD1 |START |4
3 |100 |ORD1 |START |2
4 |100 |ORD1 |INPROGRESS |3
6 |101 |ORD2 |CANCELLED |3
2 |101 |ORD2 |INPROGRESS |2
5 |101 |ORD2 |COMPLETE |1
8 |102 |ORD3 |COMPLETE |1
9 |103 |ORD4 |CANCELLED |1
10 |103 |ORD4 |CANCELLED |2

Now, the below rules should be applied to get the output..

1) If status is same, then fetch max(seq) record
2) If status is different, then apply below priority to fetch the records

Start - Priority 1 (High)
In Progress - Priority 2
Cancelled - Priority 3
Complete - Priority 4 (Lowest)

Means, the output for each order should look like below.

OID|ORDERNO|ORDERNAME|ORDERSTATUS|ORDERSEQ
3 |100 |ORD1 |START |2
2 |101 |ORD2 |INPROGRESS |2
8 |102 |ORD3 |COMPLETE |1
10 |103 |ORD4 |CANCELLED |2

how to do it.

View 1 Replies View Related

PL/SQL :: How To Update Parent And Child Tables While Updating Parent Table

Jun 13, 2012

I have a parent table EMPLOYEE which includes columns (sysid, serviceno,employeename...) sysid is Primary key, serviceno is Unique key and I have child table DEPENDENT includes columns (sysid,employee_sysid,name,dob...) here again SYSID is primary key for DEPENDENTS table, employee_sysid is Foreign key of EMPLOYEE table.

Now I want to change SYSID (using sequence) in EMPLOYEE table which need to be update in DEPENDENTS table as well

Note: I have 10000 records in EMPLOYEE table as well as I have 5 more child tables which need to update new SYSID.

View 5 Replies View Related

Parent Child Relationship

May 13, 2011

Just wanted to pose a question Can a (Unique and Not nullable)Alternate key of the parent table be part of child Table in an Identifying relationship i.e part of primary key of a child table????

Is there a rule that a primary keys of a parent table can be used to build a identifying relationship with its child?

View 1 Replies View Related

SQL & PL/SQL :: Getting Parent And Leaf Child Along With ID

Jun 14, 2012

I've the following data and i need the parent and leaf most child,including their id's

Create table par_chld (id,p_id,c_id) as
(select 1,900,501 from dual union all
select 2,900,502 from dual union all
select 3,900,503 from dual union all
select 4,100,900 from dual union all
select 5,200,900 from dual union all
select 6,300,400 from dual union all
select 7,101,500 from dual union all
select 8,102,500 from dual union all
select 9,103,500 from dual union all
select 10,201,600 from dual union all
select 11,201,601 from dual union all
select 12,201,602 from dual )

In the above data
p_id =100 has c_id 900 , and this 900 acts as a parent which has child 501 , 502 ,503

And the output should be in the following format , Where i don't need the middle level data. I need parent and its leaf child along with the id's of parent and child

Expected output

id_p p_id id_c c_id

4 100 1 501
4 100 2 502
4 100 3 503
5 200 1 501
5 200 2 502
5 200 3 503
6 300 6 400
7 101 7 500
8 102 8 500
9 103 9 500
10 201 10 600
11 201 11 601
12 201 12 602

I tried the following query where i got the p_id,id_c,c_id columns , but unable to get the column id_p

select connect_by_root p_id p_id ,id id_c,c_id
from par_chld
where connect_by_isleaf =1
start with p_id not in (select c_id
from par_chld
where c_id is not null)
connect by nocycle p_id =prior c_id

getting the id_p column as expected above.

View 2 Replies View Related

SQL & PL/SQL :: Parent-Child Hierarchy?

Dec 20, 2012

way to achieve the below logic.

SQL to pick up parent child relationship within same table with a certain logic.

Example:
mod_product_numberProduct_Hierarchy
H555888 PH05678
H888987 H555888
H8889 H555888

[code]...

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.

View 11 Replies View Related

SQL & PL/SQL :: Parent Child Relation

Sep 17, 2013

I have one requirment like below

Table1 have records like below

parent childflag
null.....A......Y
A........B......Y
B........C
C........D
D........E......Y

I want it should be like

parent child flag
null......A.....Y
A.........B.....Y
B.........E.....Y

I want to replace 'D' (parent of 'E') with any of the upper level which has Flag 'Y' (first level parent which satisfy falg 'Y')

View 7 Replies View Related

SQL & PL/SQL :: Creating Parent Child Relationship

Aug 8, 2011

I have a table called FAMILY which holda the data like , for father_gen_1 --->father_gen_2 --> father_gen_3 -- son.

father_gen_1 father_gen_2 father_gen_3 son
ABC XYZ GEF MNC
ABC lmn kth

I want to get the output which is a three column heirarical table and holds the parent child relationship Like following:-

id name parent_id
1 ABC NULL
2 XYZ 1
3 GEF 2
4 MNC 3
5 LMN 1
6 KTH 5

View 7 Replies View Related

SQL & PL/SQL :: Way Of Getting Only Parent And Child Record From Table

Jul 27, 2010

i have a table with data as follows:

select genres.* from genres

data is as follows

INTPRODUCTIDVCHGENRENAME INTGENREPAGEIDINTPARENTIDINTGENREID
14430015Biography 157 0100
14430015Classics & Poetry 173 0116
14430015Literature & Anthologies 175 173118

now when i give

select level,genres.* from genres connect by prior INTGENREPAGEID= INTPARENTID

i get

LEVELINTPRODUCTIDVCHGENRENAME INTGENREPAGEIDINTPARENTIDINTGENREID
114430015Biography 157 0100
114430015Classics & Poetry 173 0116
214430015Literature & Anthologies 175 173118
114430015Literature & Anthologies 175 173118

i need to find the parent and child from the table in this case the parent is Classics & Poetry and child is Literature & Anthologies..the way of getting only the parent and child record from this table.

View 5 Replies View Related

SQL & PL/SQL :: Parent And Child Table - Delete Row

May 18, 2010

I have a parent table and child table. I want a row to be deleted from the parent table which is referenced by a child row. Is there a way to achieve this. I dont have permission to re create the table or alter the table using delete cascade option. Is there a way to do it in sql.

SQL> create table t1(a number primary key, b number);
SQL> create table t2(c number, d number references t1(a));
SQL> insert into t1 values(1,2);
SQL> insert into t1 values(2,3);
SQL> insert into t1 values(3,4);
SQL> insert into t2 values(10,3);
SQL> insert into t2 values(20,2);
SQL> delete from t1 where a=2;
delete from t1 where a=2
*

ERROR at line 1:
ORA-02292: integrity constraint (CISBATCH.SYS_C00763501) violated - child
record found

View 11 Replies View Related

SQL & PL/SQL :: Update Parent Child Table?

May 26, 2011

i want to update primary key of 1 master and 2 child table is it possible if i do it simultaniously if not

View 9 Replies View Related

SQL & PL/SQL :: Delete - Parent / Child Relationship

May 22, 2012

I have a DB that has one parent table with many child tables. I would like to delete all records(child - parent) based upon a particular "net_id". I have three versions that "seem" to work BUT I'd like to use "version2" because I want to go to the parent one time(for the parent delete I just made it simple for me) only and be able to delete the child record(s) from the child table(s). I want to achieve this w/o using triggers...

My question: Is version2 an "ok" way to delete child records or should I try another method(version1 or 3)??

create table PARENT
(
event_id NUMBER,
event_title VARCHAR2(50),
net_id VARCHAR2(16)
);

--truncate table parent;
insert into parent
(event_id, event_title, net_id)
values
(1, 'title1', 'CURLY');
[code]...

View 10 Replies View Related

Forms :: How To Assign Value To Parent From Child Form

Feb 29, 2012

I have two forms, FormPARN, FormCHLD.

One Text item is there in FormPARN, near to that Text item, Button is there.While clicking Button FormCHLD will be opened. Inside the FormCHLD, i will do some calculation and i need to store the result in FormPARN's Text Item.

How can i do it?

View 10 Replies View Related

SQL & PL/SQL :: Roll-up Tree With Parent-child Identifiers?

Nov 16, 2011

There is table:

Column A | Column B |
X | X |
X | Y |
Y | X |
Y | Y |

I have selected view with "group by rollup":

ID | Column A | Column B |
1 | | |
2 | X | |
3 | Y | |
4 | X | X |
5 | X | Y |
6 | Y | X |
7 | Y | Y |

Now I want to display it in client app like a tree.So, I need create ID's of parents in child rows, like this:

ID | Parent ID | Column A | Column B |
1 | | | |
2 | 1 | X | |
3 | 1 | Y | |
4 | 2 | X | X |
5 | 2 | X | Y |
6 | 3 | Y | X |
7 | 3 | Y | Y |

View 7 Replies View Related

SQL & PL/SQL :: Procedure To Get Path From Parent-child Relationship?

Apr 23, 2010

I have a table which uses parent-child relationship to store the options available. I need a procedure to give me the full path given the id of a particular option.

I have two different functions. One of them uses the Oracle built in function and the other uses simple queries with a loop.The code of the functions are given below.

Now, the problem is with their "performance". The difference in their performance is significant. The function using the Oracle function takes more than 2 hours to run a query whereas the other function takes less than 2 minutes.

I am having trouble trusting the other function. No matter how many tests I perform on the output of both the functions, it always comes out to be the same.

Function 1
=====================
FUNCTION Gettree (opt_id IN NUMBER,i_app_id IN NUMBER)
RETURN VARCHAR2
IS
path VARCHAR2(32767);
application_no NUMBER;
BEGIN

[code]...

View 2 Replies View Related

SQL & PL/SQL :: Finding Child And Parent Rows In Table?

Jun 6, 2011

I have table emp that contains empno, empname, mgr .what i want is a general procedure that will take empno as input and will give all the child rows and parent for entered empno.

for ex

E
A-->B-->C-->D
F-->G
H

When i will pass d as node it will return c,b,a,e,f,g,h

View 3 Replies View Related

SQL & PL/SQL :: ORA-02291 Parent / Child Relationship Error

Jan 4, 2012

I am working on a project, the insert statement is not functioning properly and I cannot find the error The error is in the insert statement for CustOrder. The error states that NULL cannot be inserted into CustOrder.CustID. I have declared CustID and its reference. I have looked over the code several times and I cannot find the problem.

If it makes a difference I am using APEX. The company has not database or program to work with a database so I am building everything from scratch.

declare
l_CustOrderID number;
l_CustID number := :P11_CustID;
begin

[code]....

View 25 Replies View Related

SQL & PL/SQL :: Join 2 Tables To Get Parent Child Data

Mar 24, 2012

I have 2 tables, ITEM_T is the base table and RELATIVE_T is the relation table which establish the relationship between parent and child.

I want to give ZZZ child as input and I want get output in below format. I tried various method, but I am not able to get the below result.

Result:

ChildParent_1Parent_2
ZZZYYY XXX
ITEM_T
PK_IDITEM_ID
PK_1XXX

[Code]...

Expected Result:

When give input as ZZZ:

ChildParent_1Parent_2
ZZZ YYY XXX

View 7 Replies View Related

SQL & PL/SQL :: Batch ID From Child Status

Mar 27, 2013

Im looking for a query which returns the batch for which all the child should either be in 'A_STATUS','B_STATUS' or 'C_STATUS'. In this query im expecting a query which returns batch 2,3 and 4.

create table batch (batchid number);
insert into batch values(1);
insert into batch values(2);
insert into batch values(3);
insert into batch values(4);

[Code]...

View 9 Replies View Related

Fetch Records In Single Select Query And Display

Mar 15, 2011

I have 3 tables, Emp(Emp_id,emp_name),dept(dept_no,dept_name),emp_dept(emp_id,dept_no). Emp tabl ehas some 20 employes id who belongs to different departments.There are few employee who belongs to multiple departments as well. I want to fetch records of emp_id, emp_name, dept_no in the following format.

Name id dept_no
Ram 101 10
20
30
Ani 201 10
20

View 1 Replies View Related

Query To Fetch Records Between Two Dates For Fixed Timings

Sep 28, 2011

I got a issue with a query to fetch records between two dates for fixed timings

Date
From 29-09-2011 to 04-10-2011
Time
From 00:00:00hrs to 08:00:00hrs

I tried the below queries, it doesnt work
select a.detectorid,sum(b.totalvolume),a.updatetime,a.averagespeed from traffic_data a left outer join volume_data b on a.traffic_id=b.traffic_data_id
where pollinterval=1 and detectorid=�AIDC_0154� and updatetime between to_date(�29-aug-2011:00:00:00�,�DD-MON-YYYY:HH24:MI:SS�)

[code]...

View 1 Replies View Related

Forms :: Hierarchical Tree In Oracle - Parent Child Relationship

May 14, 2010

I have data in a table which don't have parent child relation ship. Can I display the data in that table in hierarchical tree item in a form.

Is it a must that data should have parent child relationship to display them in hierarchical tree item in a form.

View 1 Replies View Related

XML DB :: How To Select Parent / Child Related Data From XMLTYPE Table

Jan 9, 2013

select * from V$VERSION

BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for Solaris: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

I have loaded into an Oracle table defined as XMLTYPE. I'm okay with PL-SQL and stored procedures, where I will ultimately do this processing once I get a handle on XML DB querying from an XMLTYPE table.

Based on reading the oracle documentation about XML DB, and more importantly, reading dozens of posts to this forum,I have been successful in loading the XML files into a SQL XMLTYPE table and doing simple queries against that table to retrieve some of its data so that I can then insert that data into other target oracle relational tables.

how to select parent/child data from XMLTYPE tables. I am able to follow the forum examples and can replicate the methods shown on the many example XML contents shown on this forum, but not against the XML that I have to process. I am wondering if my struggle is caused by my lack of knowledge, or by ill-formed XML content supplied to me by the educational vendor.The XML content has structured the XML content nodes in such a way that I do not seem to be able to apply the parent/child sql methods.I have been able to use for other XML examples I have tested against.

My XML file shown below represents High School Transcript data, for which I need to be able to parse out into my own oracle relational tables for that student, his personal info, and his course info, etc. i.e., for our example, which courses he has taken for which High School grade levels. The vendor-supplied XML seems to put the Courses and the High School grade level in "parallel nodes," instead of parent/child nodes, so I am struggling to be able to use SQL to differentiate which course the student took in NinthGrade versus TenthGrade.

-- WHat I would like to determine from a select statement:

LASTNAME GradeLevel COURSETITLE
=============================
Smith NinthGrade PHYS ED 101
Smith TenthGrade CALCULUS 201
Smith TenthGrade ZOOLOGY 202

(The data has been simpliied and masked, but is true to the content and is queryable).

select * from V$VERSION

BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for Solaris: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
[code]....

View 3 Replies View Related

SQL & PL/SQL :: Create Complete Hierarchical Table From Table With Only Two Columns - Parent And Child

Aug 13, 2012

We have a table in the client database that has two columns - column parent and column child. The whole hierarchy of DB table dependencies is held in this table.If Report 1 is dependent on Table A and Table A in turn is dependent on two tables Table M and Table N. Table N is dependent on table Z it will appear in the db table as,

Hierarchy Table
Parent Child
Report1Table A
Table ATable M
Table ATable N
Table NTable Z

Requirement :

From the above structure, we need to build a table which will hold the complete hierarchy by breaking it into multiple columns.The o/p should look like this

-ParentChild 1Child 2 Child 3
-Report1Table ATable M
-Report1Table ATable N Table Z

Child 1, Child 2, Child 3 ....and so on are columns.The number of tables and the no of hierarchical relationships are dynamic.

SQL Statements to create hierarchy table:

create table hierarchy (parent varchar2(20), child varchar2(20));
insert into hierarchy values ('Report1','Table A');
insert into hierarchy values ('Report1','Table B');
insert into hierarchy values ('Table A','Table M');
insert into hierarchy values ('Table B','Table N');
insert into hierarchy values ('Report2','Table P');
insert into hierarchy values ('Table M','Table X');
insert into hierarchy values ('Table N','Table Y');
insert into hierarchy values ('Report X','Table Z');

Approached already tried :

1) Using indentation : select lpad(' ',20*(level-1)) || to_char(child) P from hierarchy connect_by start with parent='Report1' connect by prior child=parent;

2)Using connect by path function :
select *
from (select parent,child,level,connect_by_isleaf as leaf, sys_connect_by_path(child,'/') as path
from hierarchy start with parent='Report1'
connect by prior child =parent) a where Leaf not in (0);

Both the approaches give the information but the hierarchy data appears in a single column.Ideally we would like data at each level to appear in a different column.

View 3 Replies View Related

PL/SQL :: Select Parent Records

Sep 3, 2012

Is there any SQL query which can return first value. I have 1 table, 2 columns: NR; created_from_nr;

Data:

Nr=L1 created_from_nr=''
Nr=L2 created_from_nr=L1
Nr=L3 created_from_nr=L2
Nr=L4 created_from_nr=L3

How can I get Nr=L1 if I know L4, with one query ?

View 3 Replies View Related

SQL & PL/SQL :: Extract Correct Value For Child Records

Mar 22, 2011

Test case: drop table test;

create table test (id number, last_name varchar2(15), first_name varchar2(15), empno varchar2(15))

select * from test;

insert into test values (143,'frank','kadel,watson','j2098,k09876');
insert into test values (143,'steve','kadel,watson','j0987i,kuy765');
[code]....

The requirement is as follows: I need to split the rows by first_name and assign the respective empno in the results child rows if there is any.

For example:- Where id = 143, the resultset should be like this.

ID LAST_NAME FIRST_NAME EMPNO
---------- --------------- --------------- ---------------
143 frank kadel j2098
143 frank watson k09876
143 steve kadel j0987i
143 steve watson kuy765

sofar, i am able to come with the query to split the records by last name but unable to find the way to extract the respective empno and assign to the splited records correctly.

SQL> select id, last_name, EXTRACTVALUE(x.COLUMN_VALUE, 'e') first_name, empno
2 from test,
3 TABLE(XMLSEQUENCE(XMLTYPE('<e><e>' || REPLACE(first_name, ',', '</e><e>') || '</e></e>')
4 .EXTRACT('e/e'))) x
5 ;
[code]...

View 8 Replies View Related

SQL & PL/SQL :: Remove Orphaned Records From Child Table

May 24, 2011

I am facing below error while doing Schema refresh from production to development. I am facing this error in development database.

IMP-00003: ORACLE error 2264 encountered
ORA-02264: name already used by an existing constraint
IMP-00017: following statement failed with ORACLE error 2264:
"ALTER TABLE "TASKS" ADD CONSTRAINT "VALUE_CK" CHECK (wrkt_k"
"ow <> 'S') ENABLE NOVALIDATE"

i guess "need to Remove the orphaned child records from the child table (foreign key relationship), and then re-execute the ALTER TABLE ENABLE CONSTRAINT command."

how to find orphaned child records from child table ?

how to resolve this error?

View 26 Replies View Related

SQL & PL/SQL :: Join For Bringing Master And Child Records Based On Condition?

Jul 30, 2013

The attachment contains the table info. The condition is that when there are childer for the master ,the master record shd be negated . Excuse me if this very easy. Example -DEL HAS children so the record with DEL and DEL SHD not be in the result query. Example -RAG HAS NO children. so the MASTER record should be taken.

Input

COL1COL2COL3COL4
MASTERDELDELP1
MASTERDELJANP2
MASTERDELAGRP3
MASTERRAGRAGP1
MASTERCENAPTP2
MASTERRUGRUGP1

My expected output
COL1COL2COL3COL4
MASTERDELJANP2
MASTERDELAGRP3
MASTERRAGRAGP1
MASTERCENAPTP2
MASTERRUGRUGP1

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved