You Asked
I have the following table:
create table fil00_components
(ac_type varchar2(3),
wuc varchar2(10),
serial varchar2(10),
tsn number(7,1),
unit_wuc varchar2(10),
unit_serial varchar2(10)
);
alter table fil00_components add primary key(ac_type, wuc, serial)
/
alter table fil00_components add foreign key(ac_type, unit_wuc, unit_serial) references fil00_components(ac_type, wuc, serial)
/
insert into fil00_components values('114', 'AA', '114051', 11877, null, null)
/
insert into fil00_components values('114', 'BE', '8111', 9059.5, 'AA', '114051')
/
insert into fil00_components values('114', 'BEB', 'K052', 8253.5, 'BE', '8111')
/
insert into fil00_components values('114', 'BEFF', 'CGA04570', 4361.7, 'BE', '8111')
/
insert into fil00_components values('114', 'AA', '114054', 10653, null, null)
/
insert into fil00_components values('114', 'BE', '8077', 9092.9, 'AA', '114054')
/
insert into fil00_components values('114', 'BEB', 'K2076', 6719.5, 'BE', '8077')
/
insert into fil00_components values('114', 'BEFF', '191', 8633.1, 'BE', '8077')
/
commit
/
Wuc value of 'AA' is the parent of wuc value 'BE' and 'BE' is the parent of both wuc values 'BEB' and 'BEFF'.
The desired out is to have the TSN values of grandparent, the parent and the children printed on the same line,
but only the grandparent serial is printed.
AA AA BE BEB BEFF
serial TSN TSN TSN TSN
----- ----- ----- ----- -----
114051 11877 9059.5 8253.5 4361.7
114054 10653.3 9092.9 6719.5 8633.1
How can I achieve this using a single SQL statement?
and Connor said...
I'm assuming the relationships are always present (if not, you just change to an outer join), but here's an example than you can modify to suit your need
SQL> with
2 grandparents as
3 ( select
4 wuc
5 ,serial
6 ,tsn
7 ,unit_wuc
8 ,unit_serial
9 from fil00_components where unit_wuc is null ),
10 parents as
11 ( select
12 g.wuc g_wuc
13 ,g.serial g_serial
14 ,g.tsn g_tsn
15 ,g.unit_wuc g_unit_wuc
16 ,g.unit_serial g_unit_serial
17 ,f.wuc p_wuc
18 ,f.serial p_serial
19 ,f.tsn p_tsn
20 ,f.unit_wuc p_unit_wuc
21 ,f.unit_serial p_unit_serial
22 from fil00_components f,
23 grandparents g
24 where f.unit_wuc = g.wuc
25 and f.unit_serial = g.serial
26 )
27 select
28 g_wuc
29 ,g_serial
30 ,g_tsn
31 ,g_unit_wuc
32 ,g_unit_serial
33 ,p_wuc
34 ,p_serial
35 ,p_tsn
36 ,p_unit_wuc
37 ,p_unit_serial
38 ,wuc
39 ,serial
40 ,tsn
41 ,unit_wuc
42 ,unit_serial
43 from parents p,
44 fil00_components f
45 where f.unit_wuc = p.p_wuc
46 and f.unit_serial = p.p_serial;
G_WUC G_SERIAL G_TSN G_UNIT_WUC G_UNIT_SER P_WUC P_SERIAL P_TSN P_UNIT_WUC P_UNIT_SER WUC SERIAL TSN UNIT_WUC UNIT_SERIA
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
AA 114051 11877 BE 8111 9059.5 AA 114051 BEB K052 8253.5 BE 8111
AA 114051 11877 BE 8111 9059.5 AA 114051 BEFF CGA04570 4361.7 BE 8111
AA 114054 10653 BE 8077 9092.9 AA 114054 BEB K2076 6719.5 BE 8077
AA 114054 10653 BE 8077 9092.9 AA 114054 BEFF 191 8633.1 BE 8077
SQL>
SQL>
SQL>
Rating
(6 ratings)
Is this answer out of date? If it is, please let us know via a Comment