Home » SQL & PL/SQL » SQL & PL/SQL » Merging two tables based on child table FK (11.2.0.1.0)
Merging two tables based on child table FK [message #626327] |
Sun, 26 October 2014 04:59 |
|
OraFerro
Messages: 433 Registered: July 2011
|
Senior Member |
|
|
Hi All,
I have a master table (lets say payment) and a child table (lets say log or distribution) that includes detail of how the master record was logged or distributed.
I need to see the records of the master table and its log or distribution from the child table in one table.
create table test_master
(
serial_no number primary key,
m_date date,
m_country number,
amount number (10,3)
);
create table test_log
(
log_ser number primary key,
log_date date,
log_type number,
log_amount number (10,3),
log_master_fk number,
log_sub1_fk number,
log_sub2_fk number
);
--the log table refers to the master table with the FK log_master_fk
ALTER TABLE test_log ADD CONSTRAINT test_master_log
FOREIGN KEY (log_master_fk) REFERENCES test_master;
insert ALL
--into test_master values (20, sysdate -20,220, 20000 )
--into test_master values (10, sysdate -10,110, 10000 )
into test_log values (1, sysdate-20, 7, 4000, 20, 24, 5)
into test_log values (2, sysdate -20, 7, 16000, 20, 24,6)
into test_log values (3, sysdate -10, 7, 3000.5, 10, 14, 15)
into test_log values (4, sysdate -10, 8, 6000.5, 10, 14, 16)
into test_log values (5, sysdate -10, 6, 999, 10, 14, 17)
SELECT * FROM DUAL;
What I want to see is:
20 06-Oct-14 10:59:50 AM 220 20000 null null null
1 06-Oct-14 11:02:33 AM 7 4000 20 24 5
2 06-Oct-14 11:02:33 AM 7 16000 20 24 6
10 16-Oct-14 10:59:50 AM 110 10000 null null null
3 16-Oct-14 11:02:33 AM 7 3000 10 14 15
4 16-Oct-14 11:02:33 AM 8 6000 10 14 16
5 16-Oct-14 11:02:33 AM 6 1000 10 14 17
Thanks,
Ferro
[Updated on: Sun, 26 October 2014 05:02] Report message to a moderator
|
|
|
Re: Merging two tables based on child table FK [message #626328 is a reply to message #626327] |
Sun, 26 October 2014 05:34 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
with t as (
select serial_no,
m_date,
m_country,
amount,
null log_master_fk,
null log_sub1_fk,
null log_sub2_fk,
serial_no grp,
0 seq
from test_master
union all
select log_ser,
log_date,
log_type,
log_amount,
log_master_fk,
log_sub1_fk,
log_sub2_fk,
log_master_fk,
log_ser
from test_log
)
select serial_no,
to_char(m_date,'dd-mon-yy hh:mi:ss am') m_date,
m_country,
amount,
log_master_fk,
log_sub1_fk,
log_sub2_fk
from t
order by t.m_date,
grp,
seq
/
SERIAL_NO M_DATE M_COUNTRY AMOUNT LOG_MASTER_FK LOG_SUB1_FK LOG_SUB2_FK
---------- --------------------- ---------- ---------- ------------- ----------- -----------
20 06-oct-14 06:18:21 am 220 20000
1 06-oct-14 06:18:21 am 7 4000 20 24 5
2 06-oct-14 06:18:21 am 7 16000 20 24 6
10 16-oct-14 06:18:21 am 110 10000
3 16-oct-14 06:18:21 am 7 3000.5 10 14 15
4 16-oct-14 06:18:21 am 8 6000.5 10 14 16
5 16-oct-14 06:18:21 am 6 999 10 14 17
7 rows selected.
SQL>
SY.
|
|
|
Goto Forum:
Current Time: Fri Apr 26 07:57:33 CDT 2024
|