PL/SQL:How I can select the rows from the 3 tables and rearrange the rows by using particular column value?
Vehicle Passing Summary table structure
PsngSmry_ID(Number),Vehicle_iD(Number),PsngSmryTime(datetime)
Vehicle table structure
Vehicle_iD(Number),VehicleName(VarChar2),VehicleType(VarChar2)
Here Vehicle_iD is the Primary Key
Equipment Table Structure
Eqpmt_id(Number),Vehicle_iD(Number),EqpmtName(VarChar2),EqpmtType(VarChar2)
Here Eqpmt_id is the Primary Key and Vehicle_iD is the foreign Key
Equipment Component Table Structure
Eqpmt_Cmpnt_id(Number) ,Eqpmt_id(Number),EqpmtCmpntName(VarChar2),EqpmtCmpntName(VarChar2),Parent_Eqpmnt_ID(Number)
Here Eqpmt_Cmpnt_id is the Primary Key and Eqpmt_id is the foreign Key
The rows in the Vehicle Passing Summary table
PsngSmry_ID Vehicle_ID PsngSmryTime
111111 80986246 2010/10/11
111112 80986247 2010/10/12
111113 80986248 2010/10/10
The rows in the Vehicle Table
Vehicle_iD VehicleName VehicleType
80986246 Lorry Four Wheeler
80986247 Van Four Wheeler
80986248 Bus Four Wheeler
The rows in the Equipment Table:
Eqpmt_id Vehicle_iD EqpmtName EqpmtType
109846 80986246 2 Axle Lorry CAR
109821 80986246 4 Axle Lorry CAR
109825 80986246 4 Axle Lorry CAR
109562 80986247 2 Axle VAn CAR
109555 80986247 3 Axle VAn CAR
109777 80986247 3 Axle VAn CAR
109587 80986248 2 Axle Bus CAR
The rows in the Equipment Component Table :
Eqpmt_Cmpnt_id Eqpmt_id EqpmtCmpntName Parent_Eqpmnt_ID
20904146 109846 Truck
20904147 109846 Truck
20904148 109846 Axle 20904146
20904159 109846 Axle 20904146
20904167 109846 Wheel 20904148
20904177 109846 Wheel 20904148
20904185 109846 Wheel 20904159
20904325 109846 Wheel 20904159
20904188 109846 Axle 20904147
20904189 109846 Axle 20904147
20904195 109846 Wheel 20904188
20904196 109846 Wheel 20904188
20904197 109846 Wheel 20904189
20904398 109846 Wheel 20904189
10904146 109562 Truck
10904147 109562 Truck
10904148 109562 Axle 10904146
10904159 109562 Axle 10904146
10904167 109562 Wheel 10904148
10904177 109562 Wheel 10904148
10904185 109562 Wheel 10904159
10904325 109562 Wheel 10904159
10904188 109562 Axle 10904147
10904189 109562 Axle 10904147
10904195 109562 Wheel 10904188
10904196 109562 Wheel 10904188
10904197 109562 Wheel 10904189
10904398 109562 Wheel 10904189
Note : In Equipment Component Table,the hierarchy will be Truck-->Axle-->Wheel.So
1.the Parent_Eqpmnt_ID of Axle is Truck's Eqpmt_Cmpnt_id. 2.the Parent_Eqpmnt_ID of Wheel is Axle's Eqpmt_Cmpnt_id.
Now I want to write the store procedure which will take "PsngSmry_ID(Number)" as input and the o/p will be in the format :
Eqpmt_Cmpnt_id Eqpmt_id EqpmtCmpntName Parent_Eqpmnt_ID
20904146 109846 Truck
20904148 109846 Axle 20904146
20904167 109846 Wheel 20904148
20904177 109846 Wheel 20904148
20904159 109846 Axle 20904146
20904185 109846 Wheel 20904159
20904325 109846 Wheel 20904159
20904147 109846 Truck
20904188 109846 Axle 20904147
20904195 109846 Wheel 20904188
20904196 109846 Wheel 20904188
20904189 109846 Axle 20904147
20904197 109846 Wheel 20904189
20904398 109846 Wheel 20904189
10904146 109562 Truck
10904148 109562 Axle 10904146
10904167 109562 Wheel 10904148
10904177 109562 Wheel 10904148
10904159 109562 Axle 10904146
10904185 109562 Wheel 10904159
10904325 开发者_运维知识库 109562 Wheel 10904159
10904147 109562 Truck
10904188 109562 Axle 10904147
10904195 109562 Wheel 10904188
10904196 109562 Wheel 10904188
10904189 109562 Axle 10904147
10904197 109562 Wheel 10904189
10904398 109562 Wheel 10904189
**Please add these columns in the o/p **
1.EqpmtName and EqpmtType from Eqpmt table
2.VehicleName and Vehicle Type from Vehicle table
3.PsngSmryTime from PassingSummary table **
Can anyone tell me the solution?
For such questions, you should always post the corresponding DDL and DML statements. I am sure anyone willing to answer the question will appreciate such an effort. Also, it would have prevented the foreign key error and probably the -1.
That said... I have constructed such statements:
drop table tq84_equipment_component;
drop table tq84_equipment;
drop table tq84_vehicle;
create table tq84_vehicle (
vehicle_id number primary key,
vehicleName varchar2(20),
vehicleType varchar2(20)
);
create table tq84_equipment (
eqpmt_id number primary key,
vehicle_id not null references tq84_vehicle,
eqpmtName varchar2(20),
eqpmtType varchar2(20)
);
create table tq84_equipment_component (
eqpmt_cmpnt_id number primary key,
eqpmt_id not null references tq84_equipment,
eqpmtCmpntName varchar2(20),
parent_eqpmnt_id null references tq84_equipment_component
);
insert into tq84_vehicle values (80986246, 'Lorry', 'Four Wheeler');
insert into tq84_vehicle values (80986247, 'Van' , 'Four Wheeler');
insert into tq84_vehicle values (80986248, 'Bus' , 'Four Wheeler');
insert into tq84_equipment values (109846, 80986246, 'Axle Lorry', 'CAR');
insert into tq84_equipment values (109562, 80986247, 'Axle Van', 'CAR');
insert into tq84_equipment values (109587, 80986248, 'Axle Bus', 'CAR');
insert into tq84_equipment_component values (20904146, 109846, 'Truck', null);
insert into tq84_equipment_component values (20904148, 109846, 'Axle' , 20904146);
insert into tq84_equipment_component values (20904167, 109846, 'Wheel', 20904148);
insert into tq84_equipment_component values (20904177, 109846, 'Wheel', 20904148);
insert into tq84_equipment_component values (20904159, 109846, 'Axle' , 20904146);
insert into tq84_equipment_component values (20904185, 109846, 'Wheel', 20904159);
insert into tq84_equipment_component values (20904325, 109846, 'Wheel', 20904159);
insert into tq84_equipment_component values (20904147, 109846, 'Truck', null);
insert into tq84_equipment_component values (20904188, 109846, 'Axle' , 20904147);
insert into tq84_equipment_component values (20904195, 109846, 'Wheel', 20904188);
insert into tq84_equipment_component values (20904196, 109846, 'Wheel', 20904188);
insert into tq84_equipment_component values (20904189, 109846, 'Axle' , 20904147);
insert into tq84_equipment_component values (20904197, 109846, 'Wheel', 20904189);
insert into tq84_equipment_component values (20904398, 109846, 'Wheel', 20904189);
insert into tq84_equipment_component values (10904146, 109562, 'Truck', null);
insert into tq84_equipment_component values (10904148, 109562, 'Axle' , 10904146);
insert into tq84_equipment_component values (10904167, 109562, 'Wheel', 10904148);
insert into tq84_equipment_component values (10904177, 109562, 'Wheel', 10904148);
insert into tq84_equipment_component values (10904159, 109562, 'Axle ', 10904146);
insert into tq84_equipment_component values (10904185, 109562, 'Wheel', 10904159);
insert into tq84_equipment_component values (10904325, 109562, 'Wheel', 10904159);
insert into tq84_equipment_component values (10904147, 109562, 'Truck', null);
insert into tq84_equipment_component values (10904188, 109562, 'Axle' , 10904147);
insert into tq84_equipment_component values (10904195, 109562, 'Wheel', 10904188);
insert into tq84_equipment_component values (10904196, 109562, 'Wheel', 10904188);
insert into tq84_equipment_component values (10904189, 109562, 'Axle' , 10904147);
insert into tq84_equipment_component values (10904197, 109562, 'Wheel', 10904189);
insert into tq84_equipment_component values (10904398, 109562, 'Wheel', 10904189);
With these data, I believe you're after something like this:
select
eqpmt_cmpnt_id,
eqpmt_id,
eqpmtCmpntName
parent_eqpmnt_id
from
tq84_equipment_component
start with
eqpmt_id in (select eqpmt_id
from tq84_equipment
where vehicle_id = 80986246)
connect by
prior eqpmt_cmpnt_id = parent_eqpmnt_id
order by 1;
Edit: changed eqpmt_id = (select ...
to eqpmt_id in (select ...
精彩评论