2011-06-22 12 views
0

超車彙總表結構PL/SQL:如何從3個表中選擇行並使用特定的列值重新排列行?

PsngSmry_ID(Number),Vehicle_iD(Number),PsngSmryTime(datetime) 

車表結構

Vehicle_iD(Number),VehicleName(VarChar2),VehicleType(VarChar2) 

這裏Vehicle_iD是主鍵

設備表結構

Eqpmt_id(Number),Vehicle_iD(Number),EqpmtName(VarChar2),EqpmtType(VarChar2) 

這裏Eqpmt_id是主鍵和Vehicle_iD是外鍵

設備部件表結構

Eqpmt_Cmpnt_id(Number) ,Eqpmt_id(Number),EqpmtCmpntName(VarChar2),EqpmtCmpntName(VarChar2),Parent_Eqpmnt_ID(Number) 

這裏Eqpmt_Cmpnt_id是主鍵和Eqpmt_id在車輛的外鍵

該行傳遞彙總表

PsngSmry_ID Vehicle_ID PsngSmryTime 
111111  80986246 2010/10/11 
111112  80986247 2010/10/12 
111113  80986248 2010/10/10 

的行在車輛表

Vehicle_iD VehicleName VehicleType 
80986246  Lorry  Four Wheeler 
80986247  Van  Four Wheeler 
80986248  Bus  Four Wheeler 

在裝備表中的行:

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 

在設備組件表中的行:

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 

注:在設備部件表,層次結構將卡車 - > Axle-- > Wheel.So

1. Axle的Parent_Eqpmnt_ID是Truck的Eqpmt_Cmpnt_id。 2. Wheel的Parent_Eqpmnt_ID是Axle的Eqpmt_Cmpnt_id。

現在我想寫的存儲過程將採取「PsngSmry_ID(編號)」作爲輸入,O/P將在格式:

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 ** 

誰能告訴我該如何解決?

+1

我要指出的是,設備組件上的Eqpmt_id FK值與參考表值(即設備上的Eqpmt_id)不匹配,但與車輛ID匹配。嗯? – Sathya

+0

每個Eqpmnt_ID都會有多個Eqpmnt_Cmpnt_ID。在上面給出的例子中,我只發佈了一個Eqquipmnt_id。如果您有任何疑問,請通知我 –

+0

我的評論中沒有包含哪些內容?你說Eqpmt_id是FK,但你的表格值卻表示它不是。 – Sathya

回答

3

對於這樣的問題,您應該總是發佈相應的DDL和DML語句。我確信任何願意回答這個問題的人都會讚賞這樣的努力。此外,它會阻止外鍵錯誤,可能是-1。

那說...我已經建造這樣的陳述:

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); 

有了這些數據,我相信你是這樣的事情後:

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; 

編輯:改變eqpmt_id = (select ...eqpmt_id in (select ...

+0

每個Vehicle_ID都沒有。的設備...我錯誤地發佈了...抱歉我的錯誤...我現在編輯它...請看看 –

+0

其實你的查詢工作正常,如果我刪除開始與子句中的子查詢,並取而代之'Eqpmt_id ='109846'。由於tq84_equipment表將沒有。每個Vehicle_id的設備,我收到此錯誤-----> ORA-01427:單行子查詢返回多個行 –

+0

@Rajesh:我編輯了select語句。 –

相關問題