繁体   English   中英

PL / SQL:如何从3个表中选择行并通过使用特定的列值重新排列行?

[英]PL/SQL:How I can select the rows from the 3 tables and rearrange the rows by using particular column value?

车辆通过摘要表结构

    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

注意:在“设备组件表”中,层次结构为“卡车->轴->车轮”。

1.车桥的Parent_Eqpmnt_ID是卡车的Eqpmt_Cmpnt_id。 2. Wheel的Parent_Eqpmnt_ID是Axle的Eqpmt_Cmpnt_id。

现在,我想编写存储过程,它将以“ PsngSmry_ID(Number)”作为输入,并且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 **

谁能告诉我解决方案?

对于此类问题,您应始终发布相应的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 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM