我的查询定义如下
select distinct
to_number("Rental"."pkRentalId") "pkRentalId",
to_char("Rental"."RentalNo") "RentalNo",
case when "RentedVehicle"."fkRegisteredVehicleId" is not null
then to_char("Vehicle"."RegistrationNumber")
else to_char("TempVehicle"."RegistrationNumber")
end "VehicleRegNo"
from
"Rental"
inner join "RentedVehicle" on "RentedVehicle"."fkRentalId"="Rental"."pkRentalId"
left join "Vehicle" on "Vehicle"."pkVehicleId"="RentedVehicle"."fkRegisteredVehicleId"
left join "TempVehicle" on "TempVehicle"."pkTempVehicleId"="RentedVehicle"."fkTempVehicleId"
我希望我的查询返回RentedVehicle From Rental
中保存的最后一辆车租用的车辆有多辆车可以抵抗单一租赁。但我希望最后一个对抗租赁。
请帮助。
我想要
pkRentalId RenatlNo VehicleRegNo
1 100002 KM123
2 100003 KM123
3 100004 LOP990
12 100005 KM123
32 100007 KM123
34 100009 LOP990
- 表
"RentedVehicle"
( "pkRentedVehicleId" NUMBER(19,0),
"fkRegisteredVehicleId" NUMBER(19,0),
"fkTempVehicleId" NUMBER(19,0),
"fkRentalId" NUMBER(19,0) NOT NULL ENABLE,
"fkOutBranchId" NUMBER(19,0) NOT NULL ENABLE,
"fkReturnBranchId" NUMBER(19,0) NOT NULL ENABLE,
"CheckOutFuel" NUMBER(19,0) NOT NULL ENABLE,
"CheckOutOdometer" NUMBER(19,0) NOT NULL ENABLE,
"CheckInFuel" NUMBER(19,0),
"CheckInOdometer" NUMBER(19,0),
"TransferPointFuel" NUMBER(19,0),
"TransferPointOdometer" NUMBER(19,0),
"TotalCustomerKilometer" NUMBER(19,0),
"fkExchangeId" NUMBER(19,0),
"IsDeleted" CHAR(1 BYTE) NOT NULL ENABLE,
"CreatedOn" DATE NOT NULL ENABLE,
"CreatedBy" NUMBER(19,0) NOT NULL ENABLE,
"LastUpdatedOn" DATE,
"LastUpdatedBy" NUMBER(19,0)
)
Rental"
( "pkRentalId" NUMBER(19,0),
"RentalNo" VARCHAR2(15 BYTE) NOT NULL ENABLE,
"fkRentalStatusId" NUMBER(19,0) NOT NULL ENABLE,
"fkRateId" NUMBER(19,0) NOT NULL ENABLE,
"fkExtraId" NUMBER(19,0),
"fkBillingId" NUMBER(19,0),
"fkRateTypeId" NUMBER(19,0) NOT NULL ENABLE,
"fkDebtorId" NUMBER(19,0) NOT NULL ENABLE,
"fkSubDebtorId" NUMBER(19,0),
"fkBranchId" NUMBER(19,0) NOT NULL ENABLE,
"fkChangeTypeId" NUMBER(19,0) NOT NULL ENABLE,
"ChangeReason" VARCHAR2(255 BYTE),
"fkFromLocationId" NUMBER(19,0),
"fkToLocationId" NUMBER(19,0),
"fkCityId" NUMBER(19,0) NOT NULL ENABLE,
"CheckedOutOn" DATE NOT NULL ENABLE,
"ExpectedCheckInOn" DATE NOT NULL ENABLE,
"CheckedInOn" DATE,
"ReportingDateTime" DATE,
"ServiceInstructions" VARCHAR2(255 BYTE),
"DropOffRemarks" VARCHAR2(255 BYTE),
"IsDeleted" CHAR(1 BYTE) NOT NULL ENABLE,
"CreatedOn" DATE NOT NULL ENABLE,
"CreatedBy" NUMBER(19,0) NOT NULL ENABLE,
"LastUpdatedOn" DATE,
"LastUpdatedBy" NUMBER(19,0)
)
答案 0 :(得分:0)
你可以尝试这件事我仍然不确定它和你的数据
select
B.pkRentalId,
B.RentalNo,
B.VehicleRegNo
from
(
select
A.pkRentalId,
A.RentalNo,
A.VehicleRegNo,
MAX(rn)
from
(select
to_number("Rental"."pkRentalId") "pkRentalId",
to_char("Rental"."RentalNo") "RentalNo",
case when "RentedVehicle"."fkRegisteredVehicleId" is not null
then to_char("Vehicle"."RegistrationNumber")
else to_char("TempVehicle"."RegistrationNumber")
end "VehicleRegNo",
ROW_NUMBER() OVER(PARTITION BY "Rental"."pkRentalId" ORDER BY "Rental"."CheckedInOn") rn
from
"Rental"
inner join "RentedVehicle" on "RentedVehicle"."fkRentalId"="Rental"."pkRentalId"
left join "Vehicle" on "Vehicle"."pkVehicleId"="RentedVehicle"."fkRegisteredVehicleId"
left join "TempVehicle" on "TempVehicle"."pkTempVehicleId"="RentedVehicle"."fkTempVehicleId"
)
A
group by A.pkRentalId,
A.RentalNo,
A.VehicleRegNo
)
B
这里我使用"Rental"."CheckedInOn"
作为车辆日期,您可以根据它进行更改。