Home » SQL & PL/SQL » SQL & PL/SQL » Insertion logic
Insertion logic [message #623133] Fri, 05 September 2014 00:43 Go to next message
shumail
Messages: 149
Registered: September 2012
Location: Canada
Senior Member
Hi All

I would really appreciate if someone help me out and solve my issue.I have the following data.

Data Sample

Drop and delete table
Drop table parent_mst;
drop table parent_dtl;
drop table p1;
drop table p2;
drop SEQUENCE p_seq;
----------------
delete from parent_mst;
delete from parent_dtl;
delete from p1;
delete from p2;

Tables and Sequence Creation
CREATE SEQUENCE p_seq
START WITH     1
INCREMENT BY   1;
 
create table p1
(
grp number(3),
nel number(4)
);
---------
create table p2
(
grp number(3),
per number(4)
);
 
create table parent_mst
(
grp number(3),
nel number(4),
ppk_seq number(5)
);
create table parent_dtl
(
grp number(3),
per number(4),
ppk_seq number(5)
);

Insertion
insert into p1
Select 100 grp,8000 nel from dual
union all
Select 100 grp,9000 nel from dual
union all
Select 200 grp,1000 nel from dual
union all
Select 300 grp,1000 nel from dual
  ;
insert into p2
select 100 , 200  from dual  union all
select 100 , 300  from dual union all
select 100 , 500  from dual union all
select 200 ,400  from dual union all
select 200 ,600  from dual
 
;

Query on p1
select * from p1;
[b]Query Result[/b]
[b]GRP, NEL[/b]
100	8000
200	1000
300	1000
100     9000


Query on p2
Select * from p2;
[b]Query Result[/b]
[b]GRP     PER[/b]
100	200
100	300
100	500
200	400
200	600
500	600


My requirement is that I need to insert records in parent_mst and parent_dtl by using data of p1 and p2 tables.
the way of populating data in tables are:
Rules
--System get all unique PER against GRP from p2 table for example for grp=100 we have PER like 200,300,500
-- Go to p1 table and match grp and if exist then create 1 record in parent_mst table against grp and NEL (unique combination) and get its column ppk_seq and insert that ppk_seq with set of data.
Required Result
Data should be inserted in parent_mst table
GRP NEL ppk_seq
100 8000 1
100 9000 2
200 1000 3
Data should be inserted in parent_dtl table
GRP PER ppk_seq

100 200 1
100 300 1
100 500 1
100 200 2
100 300 2
100 500 2
200 400 3
200 600 3

Please let me know if you have any question, Sorry about my poor English. Thanks


Lalit : Added code tags.
In future please do it yourself, read How to use [code] tags

[Updated on: Fri, 05 September 2014 02:31] by Moderator

Report message to a moderator

Re: Insertion logic [message #623135 is a reply to message #623133] Fri, 05 September 2014 01:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> insert into parent_mst (grp, nel, ppk_seq)
  2    select grp, nel, p_seq.nextval
  3    from p1
  4    where exists (select null from p2 where p2.grp=p1.grp)
  5  /

3 rows created.

SQL> insert into parent_dtl
  2    select p.grp, p2.per, p.ppk_seq
  3    from parent_mst p, p2
  4    where p2.grp = p.grp
  5  /

8 rows created.

SQL> select * from parent_mst order by ppk_seq;
       GRP        NEL    PPK_SEQ
---------- ---------- ----------
       100       9000          1
       100       8000          2
       200       1000          3

3 rows selected.

SQL> select * from parent_dtl order by ppk_seq, grp, per;
       GRP        PER    PPK_SEQ
---------- ---------- ----------
       100        200          1
       100        300          1
       100        500          1
       100        200          2
       100        300          2
       100        500          2
       200        400          3
       200        600          3

8 rows selected.

Re: Insertion logic [message #623137 is a reply to message #623135] Fri, 05 September 2014 01:47 Go to previous messageGo to next message
shumail
Messages: 149
Registered: September 2012
Location: Canada
Senior Member
Hi Michel

Thanks for the answer and your query is working awesome, but there is only one problem, in table parent_mst we already have records that have same grp and if we run below query then all others records also inserted and I need to inserted only newly created ppk_seq.
Query
select p.grp, p2.per, p.ppk_seq
from parent_mst p, p2
where p2.grp = p.grp


for example: if we already have the following data in parent_mst table then it will create additional record in parent_dtl table. See example for more details:
parent_mst.grp=100 ,NEL=6000 and PPK_SEQ=10 already exist in parent_mst and if I use the above query then this query will generate the following records:
Records
100 200 1
100 300 1
100 500 1
100 600 10


but I need only 3 records in parent_dtl table. Please advise. Thanks
Re: Insertion logic [message #623139 is a reply to message #623137] Fri, 05 September 2014 01:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This is another and different problem.
You ask for loading tables, I answer for loading tables.
For this new problem post the test case: what are the new statements you want to execute after the first load above? What is the result you then want (complete tables content)?

Re: Insertion logic [message #623192 is a reply to message #623139] Fri, 05 September 2014 11:30 Go to previous message
shumail
Messages: 149
Registered: September 2012
Location: Canada
Senior Member
Thanks Michel, I will post it. Appreciate your help.
Previous Topic: Want to create View with parameter
Next Topic: How to escape '
Goto Forum:
  


Current Time: Tue Apr 23 08:01:02 CDT 2024