我如何在postgresql

时间:2017-10-03 13:50:28

标签: postgresql

尝试添加此查询

SELECT vehicle_id, COUNT(*)
FROM vehicle_reservations
INNER JOIN vehicles on vehicle_reservations.vehicle_id = vehicles."id"
where start_datetime>now()
GROUP BY vehicle_id, vehicles.registration

作为子查询进入以下查询链接车辆ID,以获取开始日期大于今天的计数。

需要在此查询中添加尝试添加在select的末尾作为子选择以及在where子句的末尾但是会出错。

    select keedata.kpubmodels.description "Model Description", vehicles.comm_number, vehicles.chassis_no, fleet_types."name" AS "Fleet",
vehicles.registration AS "Registration", vehicles.class_name, vehicles.engine, vehicle_specifications.transmission, to_char(vehicles.rfl_date,'dd-mm-yyyy') AS "RFL Date", to_char(vehicles.delivery_date,'dd-mm-yyyy') AS "Start Date", 
to_char(vehicles.expected_off_fleet_date,'dd-mm-yyyy') AS "End Date", vehicles.mileage AS "Mileage", vehicle_utilisations.utilisation, vehicle_utilisations.future_utilisation, 
vehicle_statuses.description AS "Status"
FROM vehicles

INNER JOIN keedata.kpubmodels on vehicles.model_id = keedata.kpubmodels.mod_id
INNER JOIN keedata.kpubcolours on vehicles.colour_id = keedata.kpubcolours."id"
INNER JOIN vehicle_statuses on vehicles.vehicle_status_id = vehicle_statuses."id"
INNER JOIN fleet_types on vehicles.fleet_type_id = fleet_types."id"
INNER JOIN vehicle_utilisations on vehicles."id" = vehicle_utilisations.vehicle_id
INNER JOIN vehicle_specifications on vehicles.specification_id = vehicle_specifications."id"

where vehicles.fleet_type_id = 24 AND vehicles.vehicle_status_id = 4 or vehicles.fleet_type_id = 24 and vehicles.vehicle_status_id = 5 or 
vehicles.fleet_type_id = 24 and vehicles.vehicle_status_id = 6
ORDER BY Substring(vehicles.registration, 5, 1) ASC

有人能指出我应该加入的位置吗?

由于

1 个答案:

答案 0 :(得分:0)

我将其作为子查询添加到选择..而不是分组依据,“假”与车辆表的连接使用= ...就像这样。

 select keedata.kpubmodels.description "Model Description", 
 vehicles.comm_number, vehicles.chassis_no, fleet_types."name" AS 
 "Fleet",
 vehicles.registration AS "Registration", vehicles.class_name, 
 vehicles.engine, vehicle_specifications.transmission, 
 to_char(vehicles.rfl_date,'dd-mm-yyyy') AS "RFL Date", 
 to_char(vehicles.delivery_date,'dd-mm-yyyy') AS "Start Date", 
 to_char(vehicles.expected_off_fleet_date,'dd-mm-yyyy') AS "End Date", 
 vehicles.mileage AS "Mileage", vehicle_utilisations.utilisation, 
 vehicle_utilisations.future_utilisation, 
 vehicle_statuses.description AS "Status"
(SELECT  COUNT(*)
 FROM vehicle_reservations xx
 where xx.start_datetime>now()
 and xx.vehicle_id=vehicle.id) as "Future Reservations"

 FROM vehicles
 INNER JOIN keedata.kpubmodels on vehicles.model_id = 
   keedata.kpubmodels.mod_id
 INNER JOIN keedata.kpubcolours on vehicles.colour_id = 
   keedata.kpubcolours."id"
 INNER JOIN vehicle_statuses on vehicles.vehicle_status_id = 
   vehicle_statuses."id"
 INNER JOIN fleet_types on vehicles.fleet_type_id = fleet_types."id"
 INNER JOIN vehicle_utilisations on vehicles."id" = 
   vehicle_utilisations.vehicle_id
 INNER JOIN vehicle_specifications on vehicles.specification_id = 
   vehicle_specifications."id"

 where vehicles.fleet_type_id = 24 AND vehicles.vehicle_status_id = 4 
 or vehicles.fleet_type_id = 24 and vehicles.vehicle_status_id = 5 or 

 vehicles.fleet_type_id = 24 and vehicles.vehicle_status_id = 6
ORDER BY Substring(vehicles.registration, 5, 1) ASC