0

从下表中,我需要确定哪个城市受到的公共汽车污染最多。

路线:

route_id | departure_city | destination_city | bus_type | times_per_day
 1         2                1                  1          4
 2         1                3                  2          2
 3         3                1                  2          1
 4         1                2                  1          5
 5         1                3                  1          3

总线类型:

bus_type_id | pollution_output
 1             3
 2             7

例如,城市 2 每天四次暴露于 bus_type 1 (route_id 1) 和 bus_type 1 每天五次 (route_id 4),每天的污染输出为 27。但我基本上需要为所有城市计算这个并返回污染最大的城市,我该怎么做?

4

1 回答 1

1
SELECT city, sum(pollution) AS total_pollution
FROM (
   SELECT r.depature_city AS city
         ,b.pollution_output * r.times_per_day AS pollution
   FROM   routes   r
   JOIN   bustypes b ON b.bus_type_id = r.bus_type

   UNION ALL
   SELECT r.destination_city
         ,b.pollution_output * r.times_per_day
   FROM   routes   r
   JOIN   bustypes b ON b.bus_type_id = r.bus_type
   ) AS sub
GROUP  BY city
于 2013-03-18T21:53:24.850 回答