Postgresql按错误分组:子查询返回的多于一行用作表达式

时间:2018-12-05 18:20:35

标签: sql postgresql

我有这张桌子

![enter image description here

我需要按地址将其分组,其中包含2个不同的费率5乘车ID和2个不同的费率1乘车ID。问题是我已经尝试过了,但方法总是这样,但它总是在sql中给我这些类型的错误:

More than one row returned by a subquery used as an expression

下面是我的查询:

SELECT 
    e1.ride_id,  
    e1.rate,  
    concat(e1.service_date, ' ', e1.service_start) AS e1_timestamp_service_start,  
    concat(e1.service_date, ' ', e1.service_end) AS e1_timestamp_service_end,

    e1.fare,  
    e1.trip_total_amount,  
    e1.actual_mileage,  
    e1.calc_mileage,  
    (SELECT e1.booked_pickup FROM efleet e1 GROUP BY e1.booked_pickup) as e1_booked_pickup,  
    e1.booked_dropoff,

    e2.ride_id,  
    e2.rate,  
    concat(e2.service_date, ' ', e2.service_start) AS e2_timestamp_service_start,  
    concat(e2.service_date, ' ', e2.service_end) AS e2_timestamp_service_end,

    e2.fare,  
    e2.trip_total_amount,  
    e2.actual_mileage,  
    e2.calc_mileage,  
    e2.booked_pickup,  
    e2.booked_dropoff,

    e2.trip_total_amount - e1.trip_total_amount AS total_amount_difference,  
    e2.actual_mileage - e1.actual_mileage AS actual_mileage_difference

FROM efleet e1, efleet e2

WHERE EXTRACT(MONTH FROM e1.service_date) = 11
  AND e2.service_date BETWEEN '2018-09-01' AND '2018-11-30'
  AND e1.rate = '5'
  AND e2.rate = '1' 
  AND e1.booked_trip_start_long = e2.booked_trip_start_long
  AND e1.booked_trip_start_lat = e2.booked_trip_start_lat
  AND e1.booked_trip_end_long = e2.booked_trip_end_long
  AND e1.booked_trip_end_lat = e2.booked_trip_end_lat
  AND e1.status = 'Approved'
  AND e2.status = 'Approved'
  AND e1.service_date = e2.service_date
  AND e2.service_start BETWEEN (e1.service_start - '01:00:00') AND (e1.service_start + '01:00:00')

ORDER BY e1_booked_pickup;

0 个答案:

没有答案